|  | 
| 10 | 10 | import zipfile | 
| 11 | 11 | import re | 
| 12 | 12 | import time | 
|  | 13 | +import os | 
| 13 | 14 | 
 | 
| 14 | 15 | # All Word prefixes / namespace matches used in document.xml & core.xml | 
| 15 | 16 | # LXML doesn't actually use prefixes (just the real namespace) , but these | 
| @@ -248,32 +249,9 @@ def picture(): | 
| 248 | 249 |     inline.append(graphic) | 
| 249 | 250 |     drawing = makeelement('drawing') | 
| 250 | 251 |     drawing.append(inline) | 
|  | 252 | +    drawing = etree.fromstring('''<w:drawing xmlns:w='{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'/>''') | 
| 251 | 253 |     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 | +     | 
| 277 | 255 | 
 | 
| 278 | 256 | 
 | 
| 279 | 257 | def search(document,search): | 
| @@ -354,32 +332,21 @@ def docproperties(title,subject,creator,keywords,lastmodifiedby=None): | 
| 354 | 332 | 
 | 
| 355 | 333 | 
 | 
| 356 | 334 | 
 | 
| 357 |  | -def savedocx(document,properties,newfilename): | 
|  | 335 | +def savedocx(document,properties,docxfilename): | 
| 358 | 336 |     '''Save a modified document''' | 
| 359 |  | -    newfile = zipfile.ZipFile(newfilename,mode='w') | 
|  | 337 | +    docxfile = zipfile.ZipFile(docxfilename,mode='w') | 
| 360 | 338 |     # Write our generated document | 
| 361 | 339 |     documentstring = etree.tostring(document, pretty_print=True) | 
| 362 |  | -    newfile.writestr('word/document.xml',documentstring) | 
|  | 340 | +    docxfile.writestr('word/document.xml',documentstring) | 
| 363 | 341 |     # And it's properties | 
| 364 | 342 |     propertiesstring = etree.tostring(properties, pretty_print=True) | 
| 365 |  | -    newfile.writestr('docProps/core.xml',propertiesstring) | 
|  | 343 | +    docxfile.writestr('docProps/core.xml',propertiesstring) | 
| 366 | 344 |     # 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 | 
| 383 | 350 |     return | 
| 384 | 351 | 
 | 
| 385 | 352 | 
 | 
0 commit comments