Skip to content

Commit e08a2fd

Browse files
committed
- Work on generating document.xml.rels from scratch
1 parent baa65a7 commit e08a2fd

4 files changed

Lines changed: 60 additions & 8 deletions

File tree

HACKING.markdown

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,20 @@ Basically just look at what's there. But if you need something more specific:
3939

4040
- Functional - every function should take some inputs, return something, and not use any globals.
4141
- Google style - http://code.google.com/p/soc/wiki/PythonStyleGuide
42-
- Unit tests are handled with nose / coverage
42+
43+
# Unit Testing
44+
45+
After adding code, open **tests/test_docx.py** and add a test that calls your function and checks its output.
46+
47+
- Use **easy_install** to fetch the **nose** and **coverage** modules
48+
- Run **nosetests --with-coverage** to run all the doctests. These should all pass.
4349

4450
# Tips
4551

4652
## If Word complains about files:
4753

4854
First, determine whether Word can recover the files:
49-
- If Word cannot recover the file, you most likely have a problem with you zip file
55+
- If Word cannot recover the file, you most likely have a problem with your zip file
5056
- If Word can recover the file, you most likely have a problem with your XML
5157

5258
### Common Zipfile issues
@@ -61,6 +67,7 @@ First, determine whether Word can recover the files:
6167
- Check your namespaces - on both the tags, and the attributes
6268
- Check capitalization of tag names
6369
- Ensure you're not missing any attributes
70+
- If images or other embedded content is shown with a large red X, your relationships file is missing data.
6471

6572
#### One common debugging technique we've used before
6673

docx.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from lxml import etree
1010
import Image
1111
import zipfile
12+
import shutil
1213
import re
1314
import time
1415
import os
@@ -229,9 +230,12 @@ def picture(picname,picdescription,pixelwidth=None,pixelheight=None):
229230
'''Create a pragraph containing an image'''
230231
# http://openxmldeveloper.org/articles/462.aspx
231232

233+
# Copy the file into the media dir
234+
shutil.copyfile(picname, 'template/word/media/'+picname)
235+
232236
# Check if the user has specified a specific size
233237
if not pixelwidth or not pixelheight:
234-
# Get info from the picture itself
238+
# If not, get info from the picture itself
235239
pixelwidth,pixelheight = Image.open(picname).size[0:2]
236240

237241
# OpenXML measures on-screen objects in English Metric Units
@@ -399,13 +403,45 @@ def websettings():
399403
web.append(makeelement('allowPNG'))
400404
web.append(makeelement('doNotSaveAsSingleFile'))
401405
return web
402-
403-
def savedocx(document,properties,contenttypes,websettings,docxfilename):
406+
407+
def wordrelationships():
408+
'''Generate a Word relationships files'''
409+
relationships = makeelement('Relationships',nsprefix='r')
410+
reltypes = {
411+
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering':'numbering.xml',
412+
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles':'styles.xml',
413+
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings':'settings.xml',
414+
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings':'webSettings.xml',
415+
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image':'media/image1.png',
416+
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable':'fontTable.xml',
417+
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme':'theme/theme1.xml',
418+
}
419+
count = 1
420+
for reltype in reltypes:
421+
relationships.append(makeelement('Relationship',attributes={'Id':'rId'+str(count),'Type':reltype,'Target':reltypes[reltype]},nsprefix=None))
422+
count += 1
423+
#relationships = etree.fromstring(
424+
'''<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
425+
<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/>
426+
<Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"/>
427+
<Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
428+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml"/>
429+
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
430+
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
431+
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/>
432+
</Relationships>'''
433+
#)
434+
return relationships
435+
436+
def savedocx(document,properties,contenttypes,websettings,wordrelationships,docxfilename):
404437
'''Save a modified document'''
405438
docxfile = zipfile.ZipFile(docxfilename,mode='w')
406439
# Serialize our trees into out zip file
407-
treesandfiles = {document:'word/document.xml',properties:'docProps/core.xml',contenttypes:'[Content_Types].xml',websettings:'word/webSettings.xml'}
440+
treesandfiles = {document:'word/document.xml',properties:'docProps/core.xml',contenttypes:'[Content_Types].xml',websettings:'word/webSettings.xml',
441+
#wordrelationships:'word/rels_/document.xml.rels'
442+
}
408443
for tree in treesandfiles:
444+
print 'Saving: '+treesandfiles[tree]
409445
treestring = etree.tostring(tree, pretty_print=True)
410446
docxfile.writestr(treesandfiles[tree],treestring)
411447

example-makedocument.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
properties = docproperties('Python docx demo','A practical example of making docx from Python','Mike MacCana',['python','Office Open XML','Word'])
5858
contenttypes = contenttypes()
5959
websettings = websettings()
60+
wordrelationships = wordrelationships()
6061

6162
# Save our document
62-
savedocx(document,properties,contenttypes,websettings,'Welcome to the Python docx module.docx')
63+
savedocx(document,properties,contenttypes,websettings,wordrelationships,'Welcome to the Python docx module.docx')
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
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">
3+
<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/>
4+
<Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"/>
5+
<Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
6+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml"/>
7+
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
8+
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
9+
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/>
10+
</Relationships>

0 commit comments

Comments
 (0)