Skip to content

Commit 2f670ac

Browse files
committed
- Generate webSettings.xml manually
1 parent 75aa42c commit 2f670ac

3 files changed

Lines changed: 15 additions & 17 deletions

File tree

docx.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -365,22 +365,21 @@ def docproperties(title,subject,creator,keywords,lastmodifiedby=None):
365365
#attributes={'type':'dcterms:W3CDTF'},tagtext='2010-01-01T21:07:00Z',attributenamespace='xsi'))
366366
return docprops
367367

368-
369-
370-
def savedocx(document,properties,contenttypes,docxfilename):
368+
def websettings():
369+
'''Generate websettings'''
370+
web = makeelement('webSettings')
371+
web.append(makeelement('allowPNG'))
372+
web.append(makeelement('doNotSaveAsSingleFile'))
373+
return web
374+
375+
def savedocx(document,properties,contenttypes,websettings,docxfilename):
371376
'''Save a modified document'''
372377
docxfile = zipfile.ZipFile(docxfilename,mode='w')
373-
# Write our generated document
374-
documentstring = etree.tostring(document, pretty_print=True)
375-
docxfile.writestr('word/document.xml',documentstring)
376-
# And it's properties
377-
propertiesstring = etree.tostring(properties, pretty_print=True)
378-
docxfile.writestr('docProps/core.xml',propertiesstring)
379-
# And it's content types
380-
contenttypesstring = etree.tostring(contenttypes, pretty_print=True)
381-
print contenttypesstring
382-
docxfile.writestr('[Content_Types].xml',contenttypesstring)
383-
378+
# Serialize our trees into out zip file
379+
treesandfiles = {document:'word/document.xml',properties:'docProps/core.xml',contenttypes:'[Content_Types].xml',websettings:'word/webSettings.xml'}
380+
for tree in treesandfiles:
381+
treestring = etree.tostring(tree, pretty_print=True)
382+
docxfile.writestr(treesandfiles[tree],treestring)
384383

385384
# Add & compress support files
386385
for dirpath,dirnames,filenames in os.walk('template'):

example-makedocument.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757
properties = docproperties('Python docx demo','A practical example of making docx from Python','Mike MacCana',['python','Office Open XML','Word'])
5858
contenttypes = contenttypes()
59+
websettings = websettings()
5960

6061
# Save our document
61-
savedocx(document,properties,contenttypes,'Welcome to the Python docx module.docx')
62+
savedocx(document,properties,contenttypes,websettings,'Welcome to the Python docx module.docx')

template/word/webSettings.xml

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)