How do I add newline within text string to zipfile using python 2.7 -
writing text zipfile using python. below if section of code using this. can't figure out character need use add new lines zipfile.
if count: textx = "the following species present:" blurb = "\r\ninsert table here\r\n\r\nsa* - south america\r\nna** - north america\r\nca*** - central america" else: textx = "no species present." blurb = ""
right "if count" true, output in document looks this:
insert table here sa* - south america na** - north america ca*** - central america
i want this:
insert table here sa* - south america na** - north america ca*** - central america
below other pertinent script snippet might troubleshoot. script 600+ lines long why did not include whole thing. everyting works except piece.
replacetext = {"textspecies" : textx, "textblurb" : blurb} template = zipfile.zipfile("c:\\template.docx") response = zipfile.zipfile(results, "a") open(template.extract("word/document.xml", databasedir + "\\")) tempxmlfile: tempxml = tempxmlfile.read() key in replacetext.keys(): tempxml = tempxml.replace(str(key), str(replacetext.get(key))) open(databasedir + "\\temp.xml", "w+") tempxmlfile: tempxmlfile.write(tempxml) file in template.filelist: if not file.filename == "word/document.xml": response.writestr(file.filename, template.read(file)) response.write(databasedir + "\\temp.xml", "word/document.xml") response.close() template.close()
ideas how add new lines? tried \r, \n, \r\n, ^11. none worked.
thanks in advance.
from details give, clear want create word docx file. while docx file zip file, zip file specific content , specific rules content. of files found xml files, word/document.xml
. within xml files whitespace (including line breaks, regardless whether of unix \n or windows persuasion \r\n) irrelevant. instead have create tags word expects , fill them reasonable data.
i have put minimal word/document.xml
file 2 paragraphs here, see i'm talking (word writes these files without whitespace single line, have formatted here easier reading):
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officedocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingdrawing"> <w:body> <w:p> <w:ppr> <w:pstyle w:val="style0"/> </w:ppr> <w:r> <w:rpr></w:rpr> <w:t>this first paragraph.</w:t> </w:r> </w:p> <w:p> <w:ppr> <w:pstyle w:val="style0"/> </w:ppr> <w:r> <w:rpr></w:rpr> <w:t>this second paragraph.</w:t> </w:r> </w:p> <w:p> <w:ppr> <w:pstyle w:val="style0"/> </w:ppr> <w:r> <w:rpr></w:rpr> </w:r> </w:p> <w:sectpr> <w:type w:val="nextpage"/> <w:pgsz w:h="16838" w:w="11906"/> <w:pgmar w:bottom="1134" w:footer="0" w:gutter="0" w:header="0" w:left="1134" w:right="1134" w:top="1134"/> <w:pgnumtype w:fmt="decimal"/> <w:formprot w:val="false"/> <w:textdirection w:val="lrtb"/> </w:sectpr> </w:body> </w:document>
it seems each line such <w:p>
tag , new paragraph 1 need create new instanc eof tag information filled in.
Comments
Post a Comment