forked from mikemaccana/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docx.py
executable file
·775 lines (707 loc) · 34.3 KB
/
docx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#!/usr/bin/env python2.6
# -*- coding: utf-8 -*-
'''
Open and modify Microsoft Word 2007 docx files (called 'OpenXML' and 'Office OpenXML' by Microsoft)
Part of Python's docx module - http://github.com/mikemaccana/python-docx
See LICENSE for licensing information.
'''
from lxml import etree
import Image
import zipfile
import shutil
import re
import time
import os
from os.path import join
# 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
if not os.path.isdir(template_dir):
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'
}
def opendocx(file):
'''Open a docx file, return a document XML tree'''
mydoc = zipfile.ZipFile(file)
xmlcontent = mydoc.read('word/document.xml')
document = etree.fromstring(xmlcontent)
return document
def newdocument():
document = makeelement('document')
document.append(makeelement('body'))
return document
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
if type(nsprefix) == list:
namespacemap = {}
for prefix in nsprefix:
namespacemap[prefix] = nsprefixes[prefix]
nsprefix = nsprefix[0] # FIXME: rest of code below expects a single prefix
if nsprefix:
namespace = '{'+nsprefixes[nsprefix]+'}'
else:
# For when namespace = None
namespace = ''
newelement = etree.Element(namespace+tagname, nsmap=namespacemap)
# Add attributes with namespaces
if attributes:
# If they haven't bothered setting attribute namespace, use an empty string
# (equivalent of no namespace)
if not attrnsprefix:
# Quick hack: it seems every element that has a 'w' nsprefix for its tag uses the same prefix for it's attributes
if nsprefix == 'w':
attributenamespace = namespace
else:
attributenamespace = ''
else:
attributenamespace = '{'+nsprefixes[attrnsprefix]+'}'
for tagattribute in attributes:
newelement.set(attributenamespace+tagattribute, attributes[tagattribute])
if tagtext:
newelement.text = tagtext
return newelement
def pagebreak(type='page', orient='portrait'):
'''Insert a break, default 'page'.
See http://openxmldeveloper.org/forums/thread/4075.aspx
Return our page break element.'''
# Need to enumerate different types of page breaks.
validtypes = ['page', 'section']
if type not in validtypes:
raise ValueError('Page break style "%s" not implemented. Valid styles: %s.' % (type, validtypes))
pagebreak = makeelement('p')
if type == 'page':
run = makeelement('r')
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'})
elif 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'):
'''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
<text> / <style>. Stile is a string containing a combination od 'bui' chars
example
paratext = [
['some bold text', 'b'],
['some normal text', ''],
['some italic underlined text', 'iu'],
]
'''
# Make our elements
paragraph = makeelement('p')
if type(paratext) == list:
text = []
for pt in paratext:
if type(pt) == list:
text.append([makeelement('t',tagtext=pt[0]), pt[1]])
else:
text.append([makeelement('t',tagtext=pt), ''])
else:
text = [[makeelement('t',tagtext=paratext),''],]
pPr = makeelement('pPr')
pStyle = makeelement('pStyle',attributes={'val':style})
pJc = makeelement('jc',attributes={'val':jc})
pPr.append(pStyle)
pPr.append(pJc)
# Add the text the run, and the run to the paragraph
paragraph.append(pPr)
for t in text:
run = makeelement('r')
rPr = makeelement('rPr')
# Apply styles
if t[1].find('b') > -1:
b = makeelement('b')
rPr.append(b)
if t[1].find('u') > -1:
u = makeelement('u',attributes={'val':'single'})
rPr.append(u)
if t[1].find('i') > -1:
i = makeelement('i')
rPr.append(i)
run.append(rPr)
# Insert lastRenderedPageBreak for assistive technologies like
# document narrators to know when a page break occurred.
if breakbefore:
lastRenderedPageBreak = makeelement('lastRenderedPageBreak')
run.append(lastRenderedPageBreak)
run.append(t[0])
paragraph.append(run)
# Return the combined paragraph
return paragraph
def contenttypes():
# FIXME - doesn't quite work...read from string as temp hack...
#types = makeelement('Types',nsprefix='ct')
types = etree.fromstring('''<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"></Types>''')
parts = {
'/word/theme/theme1.xml':'application/vnd.openxmlformats-officedocument.theme+xml',
'/word/fontTable.xml':'application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml',
'/docProps/core.xml':'application/vnd.openxmlformats-package.core-properties+xml',
'/docProps/app.xml':'application/vnd.openxmlformats-officedocument.extended-properties+xml',
'/word/document.xml':'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml',
'/word/settings.xml':'application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml',
'/word/numbering.xml':'application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml',
'/word/styles.xml':'application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml',
'/word/webSettings.xml':'application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml'
}
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'}
for extension in filetypes:
types.append(makeelement('Default',nsprefix=None,attributes={'Extension':extension,'ContentType':filetypes[extension]}))
return types
def heading(headingtext,headinglevel,lang='en'):
'''Make a new heading, return the heading element'''
lmap = {
'en': 'Heading',
'it': 'Titolo',
}
# Make our elements
paragraph = makeelement('p')
pr = makeelement('pPr')
pStyle = makeelement('pStyle',attributes={'val':lmap[lang]+str(headinglevel)})
run = makeelement('r')
text = makeelement('t',tagtext=headingtext)
# Add the text the run, and the run to the paragraph
pr.append(pStyle)
run.append(text)
paragraph.append(pr)
paragraph.append(run)
# Return the combined paragraph
return paragraph
def table(contents, heading=True, colw=None, cwunit='dxa', tblw=0, twunit='auto', borders={}, celstyle=None):
'''Get a list of lists, return a table
@param list contents: A list of lists describing contents
Every item in the list can be a string or a valid
XML element itself. It can also be a list. In that case
all the listed elements will be merged into the cell.
@param bool heading: Tells whether first line should be threated as heading
or not
@param list colw: A list of interger. The list must have same element
count of content lines. Specify column Widths in
wunitS
@param string cwunit: Unit user for column width:
'pct': fifties of a percent
'dxa': twenties of a point
'nil': no width
'auto': automagically determined
@param int tblw: Table width
@param int twunit: Unit used for table width. Same as cwunit
@param dict borders: Dictionary defining table border. Supported keys are:
'top', 'left', 'bottom', 'right', 'insideH', 'insideV', 'all'
When specified, the 'all' key has precedence over others.
Each key must define a dict of border attributes:
color: The color of the border, in hex or 'auto'
space: The space, measured in points
sz: The size of the border, in eights of a point
val: The style of the border, see http://www.schemacentral.com/sc/ooxml/t-w_ST_Border.htm
@param list celstyle: Specify the style for each colum, list of dicts.
supported keys:
'align': specify the alignment, see paragraph documentation,
@return lxml.etree: Generated XML etree element
'''
table = makeelement('tbl')
columns = len(contents[0])
# Table properties
tableprops = makeelement('tblPr')
tablestyle = makeelement('tblStyle',attributes={'val':'ColorfulGrid-Accent1'})
tableprops.append(tablestyle)
tablewidth = makeelement('tblW',attributes={'w':str(tblw),'type':str(twunit)})
tableprops.append(tablewidth)
if len(borders.keys()):
tableborders = makeelement('tblBorders')
for b in ['top', 'left', 'bottom', 'right', 'insideH', 'insideV']:
if b in borders.keys() or 'all' in borders.keys():
k = 'all' if 'all' in borders.keys() else b
attrs = {}
for a in borders[k].keys():
attrs[a] = unicode(borders[k][a])
borderelem = makeelement(b,attributes=attrs)
tableborders.append(borderelem)
tableprops.append(tableborders)
tablelook = makeelement('tblLook',attributes={'val':'0400'})
tableprops.append(tablelook)
table.append(tableprops)
# Table Grid
tablegrid = makeelement('tblGrid')
for i in range(columns):
tablegrid.append(makeelement('gridCol',attributes={'w':str(colw[i]) if colw else '2390'}))
table.append(tablegrid)
# Heading Row
row = makeelement('tr')
rowprops = makeelement('trPr')
cnfStyle = makeelement('cnfStyle',attributes={'val':'000000100000'})
rowprops.append(cnfStyle)
row.append(rowprops)
if heading:
i = 0
for heading in contents[0]:
cell = makeelement('tc')
# Cell properties
cellprops = makeelement('tcPr')
if colw:
wattr = {'w':str(colw[i]),'type':cwunit}
else:
wattr = {'w':'0','type':'auto'}
cellwidth = makeelement('tcW',attributes=wattr)
cellstyle = makeelement('shd',attributes={'val':'clear','color':'auto','fill':'548DD4','themeFill':'text2','themeFillTint':'99'})
cellprops.append(cellwidth)
cellprops.append(cellstyle)
cell.append(cellprops)
# Paragraph (Content)
if not type(heading) == list and not type(heading) == tuple:
heading = [heading,]
for h in heading:
if isinstance(h, etree._Element):
cell.append(h)
else:
cell.append(paragraph(h,jc='center'))
row.append(cell)
i += 1
table.append(row)
# Contents Rows
for contentrow in contents[1 if heading else 0:]:
row = makeelement('tr')
i = 0
for content in contentrow:
cell = makeelement('tc')
# Properties
cellprops = makeelement('tcPr')
if colw:
wattr = {'w':str(colw[i]),'type':cwunit}
else:
wattr = {'w':'0','type':'auto'}
cellwidth = makeelement('tcW',attributes=wattr)
cellprops.append(cellwidth)
cell.append(cellprops)
# Paragraph (Content)
if not type(content) == list and not type(content) == tuple:
content = [content,]
for c in content:
if isinstance(c, etree._Element):
cell.append(c)
else:
if celstyle and 'align' in celstyle[i].keys():
align = celstyle[i]['align']
else:
align = 'left'
cell.append(paragraph(c,jc=align))
row.append(cell)
i += 1
table.append(row)
return table
def picture(relationshiplist, picname, picdescription, pixelwidth=None,
pixelheight=None, nochangeaspect=True, nochangearrowheads=True):
'''Take a relationshiplist, picture file name, and return a paragraph containing the image
and an updated relationshiplist'''
# http://openxmldeveloper.org/articles/462.aspx
# Create an image. Size may be specified, otherwise it will based on the
# pixel size of image. Return a paragraph containing the picture'''
# Copy the file into the media dir
media_dir = join(template_dir,'word','media')
if not os.path.isdir(media_dir):
os.mkdir(media_dir)
shutil.copyfile(picname, join(media_dir,picname))
# Check if the user has specified a size
if not pixelwidth or not pixelheight:
# If not, get info from the picture itself
pixelwidth,pixelheight = Image.open(picname).size[0:2]
# OpenXML measures on-screen objects in English Metric Units
# 1cm = 36000 EMUs
emuperpixel = 12667
width = str(pixelwidth * emuperpixel)
height = str(pixelheight * emuperpixel)
# Set relationship ID to the first available
picid = '2'
picrelid = 'rId'+str(len(relationshiplist)+1)
relationshiplist.append([
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
'media/'+picname])
# There are 3 main elements inside a picture
# 1. The Blipfill - specifies how the image fills the picture area (stretch, tile, etc.)
blipfill = makeelement('blipFill',nsprefix='pic')
blipfill.append(makeelement('blip',nsprefix='a',attrnsprefix='r',attributes={'embed':picrelid}))
stretch = makeelement('stretch',nsprefix='a')
stretch.append(makeelement('fillRect',nsprefix='a'))
blipfill.append(makeelement('srcRect',nsprefix='a'))
blipfill.append(stretch)
# 2. The non visual picture properties
nvpicpr = makeelement('nvPicPr',nsprefix='pic')
cnvpr = makeelement('cNvPr',nsprefix='pic',
attributes={'id':'0','name':'Picture 1','descr':picname})
nvpicpr.append(cnvpr)
cnvpicpr = makeelement('cNvPicPr',nsprefix='pic')
cnvpicpr.append(makeelement('picLocks', nsprefix='a',
attributes={'noChangeAspect':str(int(nochangeaspect)),
'noChangeArrowheads':str(int(nochangearrowheads))}))
nvpicpr.append(cnvpicpr)
# 3. The Shape properties
sppr = makeelement('spPr',nsprefix='pic',attributes={'bwMode':'auto'})
xfrm = makeelement('xfrm',nsprefix='a')
xfrm.append(makeelement('off',nsprefix='a',attributes={'x':'0','y':'0'}))
xfrm.append(makeelement('ext',nsprefix='a',attributes={'cx':width,'cy':height}))
prstgeom = makeelement('prstGeom',nsprefix='a',attributes={'prst':'rect'})
prstgeom.append(makeelement('avLst',nsprefix='a'))
sppr.append(xfrm)
sppr.append(prstgeom)
# Add our 3 parts to the picture element
pic = makeelement('pic',nsprefix='pic')
pic.append(nvpicpr)
pic.append(blipfill)
pic.append(sppr)
# Now make the supporting elements
# The following sequence is just: make element, then add its children
graphicdata = makeelement('graphicData',nsprefix='a',
attributes={'uri':'http://schemas.openxmlformats.org/drawingml/2006/picture'})
graphicdata.append(pic)
graphic = makeelement('graphic',nsprefix='a')
graphic.append(graphicdata)
framelocks = makeelement('graphicFrameLocks',nsprefix='a',attributes={'noChangeAspect':'1'})
framepr = makeelement('cNvGraphicFramePr',nsprefix='wp')
framepr.append(framelocks)
docpr = makeelement('docPr',nsprefix='wp',
attributes={'id':picid,'name':'Picture 1','descr':picdescription})
effectextent = makeelement('effectExtent',nsprefix='wp',
attributes={'l':'25400','t':'0','r':'0','b':'0'})
extent = makeelement('extent',nsprefix='wp',attributes={'cx':width,'cy':height})
inline = makeelement('inline',
attributes={'distT':"0",'distB':"0",'distL':"0",'distR':"0"},nsprefix='wp')
inline.append(extent)
inline.append(effectextent)
inline.append(docpr)
inline.append(framepr)
inline.append(graphic)
drawing = makeelement('drawing')
drawing.append(inline)
run = makeelement('r')
run.append(drawing)
paragraph = makeelement('p')
paragraph.append(run)
return relationshiplist,paragraph
def search(document,search):
'''Search a document for a regex, return success / fail result'''
result = False
searchre = re.compile(search)
for element in document.iter():
if element.tag == '{%s}t' % nsprefixes['w']: # t (text) elements
if element.text:
if searchre.search(element.text):
result = True
return result
def replace(document,search,replace):
'''Replace all occurences of string with a different string, return updated document'''
newdocument = document
searchre = re.compile(search)
for element in newdocument.iter():
if element.tag == '{%s}t' % nsprefixes['w']: # t (text) elements
if element.text:
if searchre.search(element.text):
element.text = re.sub(search,replace,element.text)
return newdocument
def clean(document):
""" Perform misc cleaning operations on documents.
Returns cleaned document.
"""
newdocument = document
# Clean empty text and r tags
for t in ('t', 'r'):
rmlist = []
for element in newdocument.iter():
if element.tag == '{%s}%s' % (nsprefixes['w'], t):
if not element.text and not len(element):
rmlist.append(element)
for element in rmlist:
element.getparent().remove(element)
return newdocument
def advReplace(document,search,replace,bs=3):
'''Replace all occurences of string with a different string, return updated document
This is a modified version of python-docx.replace() that takes into
account blocks of <bs> elements at a time. The replace element can also
be a string or an xml etree element.
What it does:
It searches the entire document body for text blocks.
Then scan thos text blocks for replace.
Since the text to search could be spawned across multiple text blocks,
we need to adopt some sort of algorithm to handle this situation.
The smaller matching group of blocks (up to bs) is then adopted.
If the matching group has more than one block, blocks other than first
are cleared and all the replacement text is put on first block.
Examples:
original text blocks : [ 'Hel', 'lo,', ' world!' ]
search / replace: 'Hello,' / 'Hi!'
output blocks : [ 'Hi!', '', ' world!' ]
original text blocks : [ 'Hel', 'lo,', ' world!' ]
search / replace: 'Hello, world' / 'Hi!'
output blocks : [ 'Hi!!', '', '' ]
original text blocks : [ 'Hel', 'lo,', ' world!' ]
search / replace: 'Hel' / 'Hal'
output blocks : [ 'Hal', 'lo,', ' world!' ]
@param instance document: The original document
@param str search: The text to search for (regexp)
@param mixed replace: The replacement text or lxml.etree element to
append, or a list of etree elements
@param int bs: See above
@return instance The document with replacement applied
'''
# Enables debug output
DEBUG = False
newdocument = document
# Compile the search regexp
searchre = re.compile(search)
# Will match against searchels. Searchels is a list that contains last
# n text elements found in the document. 1 < n < bs
searchels = []
for element in newdocument.iter():
if element.tag == '{%s}t' % nsprefixes['w']: # t (text) elements
if element.text:
# Add this element to searchels
searchels.append(element)
if len(searchels) > bs:
# Is searchels is too long, remove first elements
searchels.pop(0)
# Search all combinations, of searchels, starting from
# smaller up to bigger ones
# l = search lenght
# s = search start
# e = element IDs to merge
found = False
for l in range(1,len(searchels)+1):
if found:
break
#print "slen:", l
for s in range(len(searchels)):
if found:
break
if s+l <= len(searchels):
e = range(s,s+l)
#print "elems:", e
txtsearch = ''
for k in e:
txtsearch += searchels[k].text
# Searcs for the text in the whole txtsearch
match = searchre.search(txtsearch)
if match:
found = True
# I've found something :)
if DEBUG:
print "Found element!"
print "Search regexp:", searchre.pattern
print "Requested replacement:", replace
print "Matched text:", txtsearch
print "Matched text (splitted):", map(lambda i:i.text,searchels)
print "Matched at position:", match.start()
print "matched in elements:", e
if isinstance(replace, etree._Element):
print "Will replace with XML CODE"
elif type(replace) == list or type(replace) == tuple:
print "Will replace with LIST OF ELEMENTS"
else:
print "Will replace with:", re.sub(search,replace,txtsearch)
curlen = 0
replaced = False
for i in e:
curlen += len(searchels[i].text)
if curlen > match.start() and not replaced:
# The match occurred in THIS element. Puth in the
# whole replaced text
if isinstance(replace, etree._Element):
# If I'm replacing with XML, clear the text in the
# tag and append the element
searchels[i].text = re.sub(search,'',txtsearch)
searchels[i].append(replace)
elif type(replace) == list or type(replace) == tuple:
# I'm replacing with a list of etree elements
searchels[i].text = re.sub(search,'',txtsearch)
for r in replace:
searchels[i].append(r)
else:
# Replacing with pure text
searchels[i].text = re.sub(search,replace,txtsearch)
replaced = True
if DEBUG:
print "Replacing in element #:", i
else:
# Clears the other text elements
searchels[i].text = ''
return newdocument
def getdocumenttext(document):
'''Return the raw text of a document, as a list of paragraphs.'''
paratextlist=[]
# Compile a list of all paragraph (p) elements
paralist = []
for element in document.iter():
# Find p (paragraph) elements
if element.tag == '{'+nsprefixes['w']+'}p':
paralist.append(element)
# Since a single sentence might be spread over multiple text elements, iterate through each
# paragraph, appending all text (t) children to that paragraphs text.
for para in paralist:
paratext=u''
# Loop through each paragraph
for element in para.iter():
# Find t (text) elements
if element.tag == '{'+nsprefixes['w']+'}t':
if element.text:
paratext = paratext+element.text
# Add our completed paragraph text to the list of paragraph text
if not len(paratext) == 0:
paratextlist.append(paratext)
return paratextlist
def coreproperties(title,subject,creator,keywords,lastmodifiedby=None):
'''Create core properties (common document properties referred to in the 'Dublin Core' specification).
See appproperties() for other stuff.'''
coreprops = makeelement('coreProperties',nsprefix='cp')
coreprops.append(makeelement('title',tagtext=title,nsprefix='dc'))
coreprops.append(makeelement('subject',tagtext=subject,nsprefix='dc'))
coreprops.append(makeelement('creator',tagtext=creator,nsprefix='dc'))
coreprops.append(makeelement('keywords',tagtext=','.join(keywords),nsprefix='cp'))
if not lastmodifiedby:
lastmodifiedby = creator
coreprops.append(makeelement('lastModifiedBy',tagtext=lastmodifiedby,nsprefix='cp'))
coreprops.append(makeelement('revision',tagtext='1',nsprefix='cp'))
coreprops.append(makeelement('category',tagtext='Examples',nsprefix='cp'))
coreprops.append(makeelement('description',tagtext='Examples',nsprefix='dc'))
currenttime = time.strftime('%Y-%m-%dT%H:%M:%SZ')
# Document creation and modify times
# Prob here: we have an attribute who name uses one namespace, and that
# attribute's value uses another namespace.
# We're creating the lement from a string as a workaround...
for doctime in ['created','modified']:
coreprops.append(etree.fromstring('''<dcterms:'''+doctime+''' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dcterms="http://purl.org/dc/terms/" xsi:type="dcterms:W3CDTF">'''+currenttime+'''</dcterms:'''+doctime+'''>'''))
pass
return coreprops
def appproperties():
'''Create app-specific properties. See docproperties() for more common document properties.'''
appprops = makeelement('Properties',nsprefix='ep')
appprops = etree.fromstring(
'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"></Properties>''')
props = {
'Template':'Normal.dotm',
'TotalTime':'6',
'Pages':'1',
'Words':'83',
'Characters':'475',
'Application':'Microsoft Word 12.0.0',
'DocSecurity':'0',
'Lines':'12',
'Paragraphs':'8',
'ScaleCrop':'false',
'LinksUpToDate':'false',
'CharactersWithSpaces':'583',
'SharedDoc':'false',
'HyperlinksChanged':'false',
'AppVersion':'12.0000',
}
for prop in props:
appprops.append(makeelement(prop,tagtext=props[prop],nsprefix=None))
return appprops
def websettings():
'''Generate websettings'''
web = makeelement('webSettings')
web.append(makeelement('allowPNG'))
web.append(makeelement('doNotSaveAsSingleFile'))
return web
def relationshiplist():
relationshiplist = [
['http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering','numbering.xml'],
['http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles','styles.xml'],
['http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings','settings.xml'],
['http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings','webSettings.xml'],
['http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable','fontTable.xml'],
['http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme','theme/theme1.xml'],
]
return relationshiplist
def wordrelationships(relationshiplist):
'''Generate a Word relationships file'''
# Default list of relationships
# FIXME: using string hack instead of making element
#relationships = makeelement('Relationships',nsprefix='pr')
relationships = etree.fromstring(
'''<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
</Relationships>'''
)
count = 0
for relationship in relationshiplist:
# Relationship IDs (rId) start at 1.
relationships.append(makeelement('Relationship',attributes={'Id':'rId'+str(count+1),
'Type':relationship[0],'Target':relationship[1]},nsprefix=None))
count += 1
return relationships
def savedocx(document,coreprops,appprops,contenttypes,websettings,wordrelationships,docxfilename):
'''Save a modified document'''
assert os.path.isdir(template_dir)
docxfile = zipfile.ZipFile(docxfilename,mode='w',compression=zipfile.ZIP_DEFLATED)
# Move to the template data path
prev_dir = os.path.abspath('.') # save previous working dir
os.chdir(template_dir)
# Serialize our trees into out zip file
treesandfiles = {document:'word/document.xml',
coreprops:'docProps/core.xml',
appprops:'docProps/app.xml',
contenttypes:'[Content_Types].xml',
websettings:'word/webSettings.xml',
wordrelationships:'word/_rels/document.xml.rels'}
for tree in treesandfiles:
print 'Saving: '+treesandfiles[tree]
treestring = etree.tostring(tree, pretty_print=True)
docxfile.writestr(treesandfiles[tree],treestring)
# Add & compress support files
files_to_ignore = ['.DS_Store'] # nuisance from some os's
for dirpath,dirnames,filenames in os.walk('.'):
for filename in filenames:
if filename in files_to_ignore:
continue
templatefile = join(dirpath,filename)
archivename = templatefile[2:]
print 'Saving: '+archivename
docxfile.write(templatefile, archivename)
print 'Saved new file to: '+docxfilename
os.chdir(prev_dir) # restore previous working dir
return