Adding new lines to word document using zipfile module within python 2.7?

accessnewbie at gmail.com accessnewbie at gmail.com
Tue Aug 27 16:45:27 EDT 2013


Writing text to a word document (word 2007) using the zipfile module via python coding (python 2.7). Below if the section of code I am using to do this. I can't figure out what character I need to use to get it to add new lines to the zipfile.

if Count:
    blurb = "\r\nINSERT TABLE HERE\r\n\r\nSA* - South America\r\nNA** - North America\r\nCA*** - Central America"
else:
    blurb = ""

further down I also have the following code (a small snippet of actual code for clarification)

replaceText = {"TEXTBLURB" : blurb}

Right now "if Count" is true, the output in the word document looks like this:

INSERT TABLE HERE  SA* - South America  NA** - North America  CA*** - Central America

I want it too look like this:

INSERT TABLE HERE

SA* - South America
NA** - North America
CA*** - Central America

I have a MS Word template file called "Automated Response.docx" that has a TAG imbedded between two paragraphs called "TEXTBLURB". Using a python script, if a certain condition is met, I want to replace this tag with some text (see above "blurb") that contains newlines, otherwise, don't add the text between paragraphs. I can't figure out the python code needed to imbed newlines into the tag.

Ideas as to how to add new lines via python coding? I tried \r, \n, \r\n, ^11
 and 
. None worked.



More information about the Python-list mailing list