|
9 | 9 | from lxml import etree |
10 | 10 | import Image |
11 | 11 | import zipfile |
| 12 | +import shutil |
12 | 13 | import re |
13 | 14 | import time |
14 | 15 | import os |
@@ -229,9 +230,12 @@ def picture(picname,picdescription,pixelwidth=None,pixelheight=None): |
229 | 230 | '''Create a pragraph containing an image''' |
230 | 231 | # http://openxmldeveloper.org/articles/462.aspx |
231 | 232 |
|
| 233 | + # Copy the file into the media dir |
| 234 | + shutil.copyfile(picname, 'template/word/media/'+picname) |
| 235 | + |
232 | 236 | # Check if the user has specified a specific size |
233 | 237 | if not pixelwidth or not pixelheight: |
234 | | - # Get info from the picture itself |
| 238 | + # If not, get info from the picture itself |
235 | 239 | pixelwidth,pixelheight = Image.open(picname).size[0:2] |
236 | 240 |
|
237 | 241 | # OpenXML measures on-screen objects in English Metric Units |
@@ -399,13 +403,45 @@ def websettings(): |
399 | 403 | web.append(makeelement('allowPNG')) |
400 | 404 | web.append(makeelement('doNotSaveAsSingleFile')) |
401 | 405 | 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): |
404 | 437 | '''Save a modified document''' |
405 | 438 | docxfile = zipfile.ZipFile(docxfilename,mode='w') |
406 | 439 | # 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 | + } |
408 | 443 | for tree in treesandfiles: |
| 444 | + print 'Saving: '+treesandfiles[tree] |
409 | 445 | treestring = etree.tostring(tree, pretty_print=True) |
410 | 446 | docxfile.writestr(treesandfiles[tree],treestring) |
411 | 447 |
|
|
0 commit comments