From a505bc732a9a1e350d80475f19c18af334cf1465 Mon Sep 17 00:00:00 2001 From: Steve Canny Date: Wed, 9 Jan 2013 13:31:21 -0800 Subject: [PATCH 01/25] add 'jpg' default content type --- docx.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docx.py b/docx.py index 0f41b09..79aa185 100755 --- a/docx.py +++ b/docx.py @@ -215,7 +215,14 @@ def contenttypes(): for part in parts: types.append(makeelement('Override',nsprefix=None,attributes={'PartName':part,'ContentType':parts[part]})) # Add support for filetypes - filetypes = {'rels':'application/vnd.openxmlformats-package.relationships+xml','xml':'application/xml','jpeg':'image/jpeg','gif':'image/gif','png':'image/png'} + filetypes =\ + { 'gif' : 'image/gif' + , 'jpeg' : 'image/jpeg' + , 'jpg' : 'image/jpeg' + , 'png' : 'image/png' + , 'rels' : 'application/vnd.openxmlformats-package.relationships+xml' + , 'xml' : 'application/xml' + } for extension in filetypes: types.append(makeelement('Default',nsprefix=None,attributes={'Extension':extension,'ContentType':filetypes[extension]})) return types From 0c4379c6b89a7c745890e940a9be88b04147dfc2 Mon Sep 17 00:00:00 2001 From: Steve Canny Date: Sat, 19 Jan 2013 22:05:20 -0800 Subject: [PATCH 02/25] PEP8 formatting and whitespace adjustments --- docx.py | 488 ++++++++++++++++++++-------------------- example-makedocument.py | 124 ++++++---- 2 files changed, 321 insertions(+), 291 deletions(-) diff --git a/docx.py b/docx.py index 79aa185..b573b49 100755 --- a/docx.py +++ b/docx.py @@ -24,40 +24,41 @@ # Record template directory's location which is just 'template' for a docx # developer or 'site-packages/docx-template' if you have installed docx -template_dir = join(os.path.dirname(__file__),'docx-template') # installed +template_dir = join(os.path.dirname(__file__), 'docx-template') # installed if not os.path.isdir(template_dir): - template_dir = join(os.path.dirname(__file__),'template') # dev + template_dir = join(os.path.dirname(__file__), 'template') # dev # All Word prefixes / namespace matches used in document.xml & core.xml. # LXML doesn't actually use prefixes (just the real namespace) , but these # make it easier to copy Word output more easily. -nsprefixes = { - # Text Content - 'mv':'urn:schemas-microsoft-com:mac:vml', - 'mo':'http://schemas.microsoft.com/office/mac/office/2008/main', - 've':'http://schemas.openxmlformats.org/markup-compatibility/2006', - 'o':'urn:schemas-microsoft-com:office:office', - 'r':'http://schemas.openxmlformats.org/officeDocument/2006/relationships', - 'm':'http://schemas.openxmlformats.org/officeDocument/2006/math', - 'v':'urn:schemas-microsoft-com:vml', - 'w':'http://schemas.openxmlformats.org/wordprocessingml/2006/main', - 'w10':'urn:schemas-microsoft-com:office:word', - 'wne':'http://schemas.microsoft.com/office/word/2006/wordml', - # Drawing - 'wp':'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing', - 'a':'http://schemas.openxmlformats.org/drawingml/2006/main', - 'pic':'http://schemas.openxmlformats.org/drawingml/2006/picture', - # Properties (core and extended) - 'cp':"http://schemas.openxmlformats.org/package/2006/metadata/core-properties", - 'dc':"http://purl.org/dc/elements/1.1/", - 'dcterms':"http://purl.org/dc/terms/", - 'dcmitype':"http://purl.org/dc/dcmitype/", - 'xsi':"http://www.w3.org/2001/XMLSchema-instance", - 'ep':'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties', - # Content Types (we're just making up our own namespaces here to save time) - 'ct':'http://schemas.openxmlformats.org/package/2006/content-types', - # Package Relationships (we're just making up our own namespaces here to save time) - 'pr':'http://schemas.openxmlformats.org/package/2006/relationships' +nsprefixes =\ + { 'mo' : 'http://schemas.microsoft.com/office/mac/office/2008/main' + , 'o' : 'urn:schemas-microsoft-com:office:office' + , 've' : 'http://schemas.openxmlformats.org/markup-compatibility/2006' + # Text Content + , 'w' : 'http://schemas.openxmlformats.org/wordprocessingml/2006/main' + , 'w10' : 'urn:schemas-microsoft-com:office:word' + , 'wne' : 'http://schemas.microsoft.com/office/word/2006/wordml' + # Drawing + , 'a' : 'http://schemas.openxmlformats.org/drawingml/2006/main' + , 'm' : 'http://schemas.openxmlformats.org/officeDocument/2006/math' + , 'mv' : 'urn:schemas-microsoft-com:mac:vml' + , 'pic' : 'http://schemas.openxmlformats.org/drawingml/2006/picture' + , 'v' : 'urn:schemas-microsoft-com:vml' + , 'wp' : 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing' + # Properties (core and extended) + , 'cp' : 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties' + , 'dc' : 'http://purl.org/dc/elements/1.1/' + , 'ep' : 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties' + , 'xsi' : 'http://www.w3.org/2001/XMLSchema-instance' + # Content Types (we're just making up our own namespaces here to save time) + , 'ct' : 'http://schemas.openxmlformats.org/package/2006/content-types' + # Package Relationships (we're just making up our own namespaces here to save time) + , 'r' : 'http://schemas.openxmlformats.org/officeDocument/2006/relationships' + , 'pr' : 'http://schemas.openxmlformats.org/package/2006/relationships' + # Dublin Core document properties + , 'dcmitype' :'http://purl.org/dc/dcmitype/' + , 'dcterms' :'http://purl.org/dc/terms/' } def opendocx(file): @@ -72,7 +73,7 @@ def newdocument(): document.append(makeelement('body')) return document -def makeelement(tagname,tagtext=None,nsprefix='w',attributes=None,attrnsprefix=None): +def makeelement(tagname, tagtext=None, nsprefix='w', attributes=None, attrnsprefix=None): '''Create an element & return it''' # Deal with list of nsprefix by making namespacemap namespacemap = None @@ -99,7 +100,7 @@ def makeelement(tagname,tagtext=None,nsprefix='w',attributes=None,attrnsprefix=N attributenamespace = '' else: attributenamespace = '{'+nsprefixes[attrnsprefix]+'}' - + for tagattribute in attributes: newelement.set(attributenamespace+tagattribute, attributes[tagattribute]) if tagtext: @@ -117,60 +118,60 @@ def pagebreak(type='page', orient='portrait'): pagebreak = makeelement('p') if type == 'page': run = makeelement('r') - br = makeelement('br',attributes={'type':type}) + br = makeelement('br', attributes={'type':type}) run.append(br) pagebreak.append(run) elif type == 'section': pPr = makeelement('pPr') sectPr = makeelement('sectPr') if orient == 'portrait': - pgSz = makeelement('pgSz',attributes={'w':'12240','h':'15840'}) + pgSz = makeelement('pgSz', attributes={'w':'12240', 'h':'15840'}) elif orient == 'landscape': - pgSz = makeelement('pgSz',attributes={'h':'12240','w':'15840', 'orient':'landscape'}) + pgSz = makeelement('pgSz', attributes={'h':'12240', 'w':'15840', 'orient':'landscape'}) sectPr.append(pgSz) pPr.append(sectPr) pagebreak.append(pPr) return pagebreak -def paragraph(paratext,style='BodyText',breakbefore=False,jc='left'): +def paragraph(paratext, style='BodyText', breakbefore=False, jc='left'): '''Make a new paragraph element, containing a run, and some text. Return the paragraph element. - + @param string jc: Paragraph alignment, possible values: left, center, right, both (justified), ... see http://www.schemacentral.com/sc/ooxml/t-w_ST_Jc.html for a full list - + If paratext is a list, spawn multiple run/text elements. Support text styles (paratext must then be a list of lists in the form /