forked from mikemaccana/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docx.py
executable file
·1109 lines (982 loc) · 43.4 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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: 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.
"""
import os
import re
import time
import shutil
import zipfile
from lxml import etree
from os.path import abspath, basename, join
from numpy import unicode_
try:
from PIL import Image
except ImportError:
import Image
try:
from PIL.ExifTags import TAGS
except ImportError:
TAGS = {}
from warnings import PendingDeprecationWarning
from warnings import warn
import logging
log = logging.getLogger(__name__)
# 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 = {
'mo': 'http://schemas.microsoft.com/office/mac/office/2008/main',
'o': 'urn:schemas-microsoft-com:office:office',
've': 'http://schemas.openxmlformats.org/markup-compatibility/2006',
# Text Content
'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
'a': 'http://schemas.openxmlformats.org/drawingml/2006/main',
'm': 'http://schemas.openxmlformats.org/officeDocument/2006/math',
'mv': 'urn:schemas-microsoft-com:mac:vml',
'pic': 'http://schemas.openxmlformats.org/drawingml/2006/picture',
'v': 'urn:schemas-microsoft-com:vml',
'wp': ('http://schemas.openxmlformats.org/drawingml/2006/wordprocessing'
'Drawing'),
# Properties (core and extended)
'cp': ('http://schemas.openxmlformats.org/package/2006/metadata/core-pr'
'operties'),
'dc': 'http://purl.org/dc/elements/1.1/',
'ep': ('http://schemas.openxmlformats.org/officeDocument/2006/extended-'
'properties'),
'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
# Content Types
'ct': 'http://schemas.openxmlformats.org/package/2006/content-types',
# Package Relationships
'r': ('http://schemas.openxmlformats.org/officeDocument/2006/relationsh'
'ips'),
'pr': 'http://schemas.openxmlformats.org/package/2006/relationships',
# Dublin Core document properties
'dcmitype': 'http://purl.org/dc/dcmitype/',
'dcterms': 'http://purl.org/dc/terms/'}
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 isinstance(nsprefix, list):
namespacemap = {}
for prefix in nsprefix:
namespacemap[prefix] = nsprefixes[prefix]
# FIXME: rest of code below expects a single prefix
nsprefix = nsprefix[0]
if nsprefix:
namespace = '{%s}' % 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:
tmpl = 'Page break style "%s" not implemented. Valid styles: %s.'
raise ValueError(tmpl % (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'):
"""
Return a new paragraph element containing *paratext*. The paragraph's
default style is 'Body Text', but a new style may be set using the
*style* parameter.
@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, add a run for each (text, char_format_str)
2-tuple in the list. char_format_str is a string containing one or more
of the characters 'b', 'i', or 'u', meaning bold, italic, and underline
respectively. For example:
paratext = [
('some bold text', 'b'),
('some normal text', ''),
('some italic underlined text', 'iu')
]
"""
# Make our elements
paragraph = makeelement('p')
if not isinstance(paratext, list):
paratext = [(paratext, '')]
text_tuples = []
for pt in paratext:
text, char_styles_str = (pt if isinstance(pt, (list, tuple))
else (pt, ''))
text_elm = makeelement('t', tagtext=text)
if len(text.strip()) < len(text):
text_elm.set('{http://www.w3.org/XML/1998/namespace}space',
'preserve')
text_tuples.append([text_elm, char_styles_str])
pPr = makeelement('pPr')
pStyle = makeelement('pStyle', attributes={'val': style})
pJc = makeelement('jc', attributes={'val': jc})
pPr.append(pStyle)
pPr.append(pJc)
# Add the text to the run, and the run to the paragraph
paragraph.append(pPr)
for text_elm, char_styles_str in text_tuples:
run = makeelement('r')
rPr = makeelement('rPr')
# Apply styles
if 'b' in char_styles_str:
b = makeelement('b')
rPr.append(b)
if 'i' in char_styles_str:
i = makeelement('i')
rPr.append(i)
if 'u' in char_styles_str:
u = makeelement('u', attributes={'val': 'single'})
rPr.append(u)
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(text_elm)
paragraph.append(run)
# Return the combined paragraph
return paragraph
def contenttypes():
types = etree.fromstring(
'<Types xmlns="http://schemas.openxmlformats.org/package/2006/conten'
't-types"></Types>')
parts = {
'/word/theme/theme1.xml': 'application/vnd.openxmlformats-officedocu'
'ment.theme+xml',
'/word/fontTable.xml': 'application/vnd.openxmlformats-officedocu'
'ment.wordprocessingml.fontTable+xml',
'/docProps/core.xml': 'application/vnd.openxmlformats-package.co'
're-properties+xml',
'/docProps/app.xml': 'application/vnd.openxmlformats-officedocu'
'ment.extended-properties+xml',
'/word/document.xml': 'application/vnd.openxmlformats-officedocu'
'ment.wordprocessingml.document.main+xml',
'/word/settings.xml': 'application/vnd.openxmlformats-officedocu'
'ment.wordprocessingml.settings+xml',
'/word/numbering.xml': 'application/vnd.openxmlformats-officedocu'
'ment.wordprocessingml.numbering+xml',
'/word/styles.xml': 'application/vnd.openxmlformats-officedocu'
'ment.wordprocessingml.styles+xml',
'/word/webSettings.xml': 'application/vnd.openxmlformats-officedocu'
'ment.wordprocessingml.webSettings+xml'}
for part in parts:
types.append(makeelement('Override', nsprefix=None,
attributes={'PartName': part,
'ContentType': parts[part]}))
# Add support for filetypes
filetypes = {
'gif': 'image/gif',
'jpeg': 'image/jpeg',
'jpg': 'image/jpeg',
'png': 'image/png',
'rels': 'application/vnd.openxmlformats-package.relationships+xml',
'xml': 'application/xml'
}
for extension in filetypes:
attrs = {
'Extension': extension,
'ContentType': filetypes[extension]
}
default_elm = makeelement('Default', nsprefix=None, attributes=attrs)
types.append(default_elm)
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):
"""
Return a table element based on specified parameters
@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 treated as
heading or not
@param list colw: list of integer column widths specified in wunitS.
@param str cwunit: Unit used for column width:
'pct' : fiftieths of a percent
'dxa' : twentieths of a point
'nil' : no width
'auto' : automagically determined
@param int tblw: Table width
@param str twunit: Unit used for table width. Same possible values 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 eighths 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': ''})
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):
attrs = {'w': str(colw[i]) if colw else '2390'}
tablegrid.append(makeelement('gridCol', attributes=attrs))
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': 'FFFFFF',
'themeFill': 'text2',
'themeFillTint': '99'})
cellprops.append(cellwidth)
cellprops.append(cellstyle)
cell.append(cellprops)
# Paragraph (Content)
if not isinstance(heading, (list, 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 isinstance(content, (list, 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,
imagefiledict=None):
"""
Take a relationshiplist, picture file name, and return a paragraph
containing the image and an updated relationshiplist
"""
if imagefiledict is None:
warn(
'Using picture() without imagefiledict parameter will be depreca'
'ted in the future.', PendingDeprecationWarning
)
# 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
# Set relationship ID to that of the image or the first available one
picid = '2'
picpath = abspath(picname)
if imagefiledict is not None:
# Keep track of the image files in a separate dictionary so they don't
# need to be copied into the template directory
if picpath not in imagefiledict:
picrelid = 'rId' + str(len(relationshiplist) + 1)
imagefiledict[picpath] = picrelid
relationshiplist.append([
'http://schemas.openxmlformats.org/officeDocument/2006/relat'
'ionships/image',
'media/%s_%s' % (picrelid, basename(picpath))
])
else:
picrelid = imagefiledict[picpath]
else:
# Copy files into template directory for backwards compatibility
# Images still accumulate in the template directory this way
picrelid = 'rId' + str(len(relationshiplist) + 1)
relationshiplist.append([
'http://schemas.openxmlformats.org/officeDocument/2006/relations'
'hips/image', 'media/' + picname
])
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))
image = Image.open(picpath)
# Extract EXIF data, if available
try:
exif = image._getexif()
exif = {} if exif is None else exif
except:
exif = {}
imageExif = {}
for tag, value in exif.items():
imageExif[TAGS.get(tag, tag)] = value
imageOrientation = imageExif.get('Orientation', 1)
imageAngle = {
1: 0, 2: 0, 3: 180, 4: 0, 5: 90, 6: 90, 7: 270, 8: 270
}[imageOrientation]
imageFlipH = 'true' if imageOrientation in (2, 5, 7) else 'false'
imageFlipV = 'true' if imageOrientation == 4 else 'false'
# 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.size[0:2]
# Swap width and height if necessary
if imageOrientation in (5, 6, 7, 8):
pixelwidth, pixelheight = pixelheight, pixelwidth
# OpenXML measures on-screen objects in English Metric Units
# 1cm = 36000 EMUs
emuperpixel = 12700
width = str(pixelwidth * emuperpixel)
height = str(pixelheight * emuperpixel)
# 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': picdescription}
)
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', attributes={
'rot': str(imageAngle * 60000), 'flipH': imageFlipH,
'flipV': imageFlipV
}
)
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/200'
'6/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)
if imagefiledict is not None:
return relationshiplist, paragraph, imagefiledict
else:
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 findTypeParent(element, tag):
""" Finds fist parent of element of the given type
@param object element: etree element
@param string the tag parent to search for
@return object element: the found parent or None when not found
"""
p = element
while True:
p = p.getparent()
if p.tag == tag:
return p
# Not found
return None
def AdvSearch(document, search, bs=3):
'''Return set of all regex matches
This is an advanced version of python-docx.search() that takes into
account blocks of <bs> elements at a time.
What it does:
It searches the entire document body for text blocks.
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 : 'Hello,'
output blocks : [ 'Hello,' ]
original text blocks : [ 'Hel', 'lo', ' __', 'name', '__!' ]
search : '(__[a-z]+__)'
output blocks : [ '__name__' ]
@param instance document: The original document
@param str search: The text to search for (regexp)
append, or a list of etree elements
@param int bs: See above
@return set All occurences of search string
'''
# Compile the search regexp
searchre = re.compile(search)
matches = []
# 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 document.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
for s in range(len(searchels)):
if found:
break
if s+l <= len(searchels):
e = range(s, s+l)
txtsearch = ''
for k in e:
txtsearch += searchels[k].text
# Searcs for the text in the whole txtsearch
match = searchre.search(txtsearch)
if match:
matches.append(match.group())
found = True
return set(matches)
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:
log.debug("Found element!")
log.debug("Search regexp: %s",
searchre.pattern)
log.debug("Requested replacement: %s",
replace)
log.debug("Matched text: %s", txtsearch)
log.debug("Matched text (splitted): %s",
map(lambda i: i.text, searchels))
log.debug("Matched at position: %s",
match.start())
log.debug("matched in elements: %s", e)
if isinstance(replace, etree._Element):
log.debug("Will replace with XML CODE")
elif isinstance(replace(list, tuple)):
log.debug("Will replace with LIST OF"
" ELEMENTS")
else:
log.debug("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):
# Convert to a list and process
# it later
replace = [replace]
if isinstance(replace, (list, tuple)):
# I'm replacing with a list of
# etree elements
# clear the text in the tag and
# append the element after the
# parent paragraph
# (because t elements cannot have
# childs)
p = findTypeParent(
searchels[i],
'{%s}p' % nsprefixes['w'])
searchels[i].text = re.sub(
search, '', txtsearch)
insindex = p.getparent().index(p)+1
for r in replace:
p.getparent().insert(
insindex, r)
insindex += 1
else:
# Replacing with pure text
searchels[i].text = re.sub(
search, replace, txtsearch)
replaced = True
log.debug(
"Replacing in element #: %s", 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
elif element.tag == '{'+nsprefixes['w']+'}tab':
paratext = paratext + '\t'
# 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 element from a string as a workaround...
for doctime in ['created', 'modified']:
elm_str = (
'<dcterms:%s xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc'
'e" xmlns:dcterms="http://purl.org/dc/terms/" xsi:type="dcterms:'
'W3CDTF">%s</dcterms:%s>'
) % (doctime, currenttime, doctime)
coreprops.append(etree.fromstring(elm_str))
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 x'
'mlns="http://schemas.openxmlformats.org/officeDocument/2006/extended'
'-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocum'
'ent/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',