Skip to content
This repository was archived by the owner on Jan 12, 2018. It is now read-only.

Commit d05c229

Browse files
committed
- Walk template dir for zip creation
- Further preliminary work on image creation
1 parent ce52fed commit d05c229

File tree

5 files changed

+15
-55
lines changed

5 files changed

+15
-55
lines changed

HACKING.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Adding Features
33

44
# Recommended reading
55

6-
- The LXML tutorial at http://codespeak.net/lxml/tutorial.html covers the basics of XML etrees, which we create append and insert to make XML documents
6+
- The LXML tutorial at http://codespeak.net/lxml/tutorial.html covers the basics of XML etrees, which we create, append and insert to make XML documents. LXML also provides XPath, which we use to specify locations in the document.
77
- The OpenXML WordML specs and videos at http://openxmldeveloper.org (if you're stuck)
88
- Learning about XML namespaces http://www.w3schools.com/XML/xml_namespaces.asp
99
- The Namespaces section of http://diveintopython3.org/xml.html

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2009 Mike MacCana
1+
Copyright (c) 2009-2010 Mike MacCana
22

33
Permission is hereby granted, free of charge, to any person
44
obtaining a copy of this software and associated documentation

docx.py

+12-45
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import zipfile
1111
import re
1212
import time
13+
import os
1314

1415
# All Word prefixes / namespace matches used in document.xml & core.xml
1516
# LXML doesn't actually use prefixes (just the real namespace) , but these
@@ -248,32 +249,9 @@ def picture():
248249
inline.append(graphic)
249250
drawing = makeelement('drawing')
250251
drawing.append(inline)
252+
drawing = etree.fromstring('''<w:drawing xmlns:w='{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'/>''')
251253
return drawing
252-
'''
253-
<w:drawing>
254-
<wp:inline distT="0" distB="0" distL="0" distR="0">
255-
<wp:extent cx="5486400" cy="3429000"/>
256-
<wp:effectExtent l="25400" t="0" r="0" b="0"/>
257-
<wp:docPr id="1" name="Picture 0" descr="aero_glow_v2_1920x1200.png"/>
258-
<wp:cNvGraphicFramePr>
259-
<a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
260-
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
261-
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
262-
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
263-
<pic:nvPicPr>
264-
<pic:cNvPr id="0" name="aero_glow_v2_1920x1200.png"/>
265-
<pic:cNvPicPr/>
266-
<pic:blipFill>
267-
<a:blip r:embed="rId5"/>
268-
<a:stretch>
269-
<a:fillRect/>
270-
<pic:spPr>
271-
<a:xfrm>
272-
<a:off x="0" y="0"/>
273-
<a:ext cx="5486400" cy="3429000"/>
274-
<a:prstGeom prst="rect">
275-
<a:avLst/>
276-
'''
254+
277255

278256

279257
def search(document,search):
@@ -354,32 +332,21 @@ def docproperties(title,subject,creator,keywords,lastmodifiedby=None):
354332

355333

356334

357-
def savedocx(document,properties,newfilename):
335+
def savedocx(document,properties,docxfilename):
358336
'''Save a modified document'''
359-
newfile = zipfile.ZipFile(newfilename,mode='w')
337+
docxfile = zipfile.ZipFile(docxfilename,mode='w')
360338
# Write our generated document
361339
documentstring = etree.tostring(document, pretty_print=True)
362-
newfile.writestr('word/document.xml',documentstring)
340+
docxfile.writestr('word/document.xml',documentstring)
363341
# And it's properties
364342
propertiesstring = etree.tostring(properties, pretty_print=True)
365-
newfile.writestr('docProps/core.xml',propertiesstring)
343+
docxfile.writestr('docProps/core.xml',propertiesstring)
366344
# Add support files
367-
for xmlfile in [
368-
'[Content_Types].xml',
369-
'_rels/.rels',
370-
'docProps/thumbnail.jpeg',
371-
'docProps/app.xml',
372-
'word/webSettings.xml',
373-
'word/_rels/document.xml.rels',
374-
'word/styles.xml',
375-
'word/numbering.xml',
376-
'word/theme/',
377-
'word/theme/theme1.xml',
378-
#'word/media/image1.png',
379-
'word/settings.xml',
380-
'word/fontTable.xml']:
381-
newfile.write('template/'+xmlfile,xmlfile)
382-
print 'Saved new file to: '+newfilename
345+
for dirpath,dirnames,filenames in os.walk('template'):
346+
for filename in filenames:
347+
templatefile = os.path.join(dirpath,filename)
348+
docxfile.write(templatefile,templatefile.replace('template',''))
349+
print 'Saved new file to: '+docxfilename
383350
return
384351

385352

template/word/_rels/document.xml.rels

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/><Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"/><Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/><Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/></Relationships>
2+
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/><Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/></Relationships>

template/word/document.xml

-7
This file was deleted.

0 commit comments

Comments
 (0)