Import text files into preformatted word document

3819
3
03-28-2013 09:28 AM
StefaniGermanotta
New Contributor II
I have this incredibly long python script that ultimately ends up with several text files (generated using ArcGIS 10.1) that I would like to import into a pre-formatted MS Word document/template. I want the files imported into the document and then displayed within MS Word for review.

Can this be done? And if so, what approaches would you, the python gurus, recommend?

I attached some very small dummy sample files (Lou Gehrig speech for those baseball fans) in hopes that I could get some specific code examples to go by and then modify as I progress through the text file generation scripting process, which is on-going. Let???s assume files are kept in C:\Test. Using python 2.7. MS Word 2007.

Thank you all in advance.

Files included in attachment:

SampleInsertText.docx -  A sample of the MS Word 2007 template
SampleResult.docx - A sample of the resulting word document after scripting
textfile1.txt - sample of 1st paragraph to be imported
textfile2.txt - sample of 2nd paragraph to be imported
textfile2.txt - sample of 3rd paragraph to be imported
textfile2.txt - sample of 4th paragraph to be imported
Tags (2)
0 Kudos
3 Replies
MikeMacRae
Occasional Contributor III
Try reading through the following web page. It's where I got started when I wanted to read/write to word documents using python.

http://www.galalaly.me/index.php/2011/09/use-python-to-parse-microsoft-word-documents-using-pywin32-...

This is a little out-of-scope for the ESRI website, so jumping over to Stack Overflow, you will find a ton of posts dealing with the same thing. basically, you can use the PyWin32 library of tools to learn how to write/format to MS Word. It's definitely a good python skill to have.

Mike
0 Kudos
StefaniGermanotta
New Contributor II
Try reading through the following web page. It's where I got started when I wanted to read/write to word documents using python.

http://www.galalaly.me/index.php/2011/09/use-python-to-parse-microsoft-word-documents-using-pywin32-...

This is a little out-of-scope for the ESRI website, so jumping over to Stack Overflow, you will find a ton of posts dealing with the same thing. basically, you can use the PyWin32 library of tools to learn how to write/format to MS Word. It's definitely a good python skill to have.

Mike


I was over there prior to here but could not upload files and much of what I read seemed overwhelming. I was hoping for specific coding examples as I learn best by dissecting the code and working backwards. Probably an ADD thing. (But I will review webpage-thanks)

I know it is not exactly an arcpy question and thus out-of-scope, but I figured since I created the real files by heavily relying on multiple geoprocessing results (see my other post for example) I could get away with it. (sheepish grin)
0 Kudos
GalVine
New Contributor
I was over there prior to here but could not upload files and much of what I read seemed overwhelming. I was hoping for specific coding examples as I learn best by dissecting the code and working backwards. Probably an ADD thing. (But I will review webpage-thanks)

I know it is not exactly an arcpy question and thus out-of-scope, but I figured since I created the real files by heavily relying on multiple geoprocessing results (see my other post for example) I could get away with it. (sheepish grin)




Hey!
I seem to be doing a very similar thing to what you've done, and I now started building my python code for MS word manipulation.
Did you get on top of it, and feel like sharing code and experience?

galvine@npa.org.il \\ galvine@gmail.com

Cheers,
Gal
0 Kudos