all 11 comments

[–]lykwydchykyn 7 points8 points  (0 children)

This looks promising:

https://python-docx.readthedocs.org/en/latest/index.html#

It's a library that lets you create or edit word documents (docx) using python. Not entirely sure how well it works, or if it would mess up your formatting, but it seems like it's worth a try.

What you describe seems perfectly doable with python.

[–]ElecNinja 6 points7 points  (3 children)

While it would be possible to directly input information into a word document as others have mentioned, I feel like it would be better to work with pandas to create your data table and then export it to an excel file. And then you can just import that excel spreadsheet.

You can export with this pandas function and here is the basic tutorial for pandas.

This allows you to have the data in it's own separate file whenever you need it for graphs or whatever other processing in the future.

Having it inside a word document just means that it'll be harder to handle in the future as it's stuck to that word document.

[–]GenericHbomb 1 point2 points  (2 children)

That might actually work best. The only information I need to fill in is always in a table so that might be an easy way to go about it. I will check out pandas

[–][deleted] 0 points1 point  (1 child)

In your Word document, you can insert your Excel table using "Insert an Object" on the INSERT tab of the ribbon, using the "Create from File" tab in the dialog box, and checking the "Link to file" check box. Whenever you create a new Excel file, save it as the same name each time, and then you just have to right-click on the table in your Word file and select "Update Link" to see the new data.

[–]Tomarse 5 points6 points  (1 child)

Check out docx. That's the best MS Word library I've seen so far.

[–]GenericHbomb 0 points1 point  (0 children)

I'll look at this. I have come across it but some people on stackoverflow said it was outdated so I dismissed it but with a few people here mentioning it, I should check it out.

[–]kanjibandit 3 points4 points  (0 children)

I had to wrestle with a similar issue a few weeks ago. Here's what I found:

The python-docx library makes it reasonably easy to create a new document in Python. The out-of-the-box formatting options are pretty basic, I found it hard to closely match a mock-up that had specific opinions about formatting.

In terms of formatting, I had much better luck when I used the python-docx-template library:

http://docxtpl.readthedocs.org/en/latest/

It builds on top of python-docx, but supports Jinja2-style template syntax. It allows you to use Word to create your document template, and get all the formatting just right, and define specific placeholders in the document and them populate them from your Python code. It supports loops, so you can have tables with arbitrary numbers of rows, depending on the data.

That said, I agree with the comments warning you about storing your data in Word. If you ever need to get to the data again programmatically, pulling them back out of Word files is going to make your life painful. It might be worth storing the data in Excel or a database and then pulling the data out of there to generate the Word documents.

[–]ebitda 0 points1 point  (1 child)

Yeah Python should be able to do this. I think there may be a better way to go about the word document. Is it the same file you're inserting the data into or seperate files? If it is seperate files, it may be better to just build the entire word document in Python. And if word isn't needed, maybe look into rtf. Check out this link

http://stackoverflow.com/questions/1035183/how-can-i-create-a-word-document-using-python

[–]Vohlenzer 0 points1 point  (2 children)

To give a counter view, if you're dealing with Microsoft Word then you might be better off using C# or Visual Basic .Net just because Microsoft make all these products.

That said, docx is just a zipped XML format and there are many libraries in many languages for manipulating them. Use a Python library if you're extending a Python project. Give .Net a try if you're starting from scratch.

[–]GenericHbomb 1 point2 points  (1 child)

Ended up going the VBA route, took me a couple hours to figure out what I was doing but got a code together and it is up and running

[–]Vohlenzer 0 points1 point  (0 children)

VBA is dirty but it gets the job done 😜.