/
preso.py
1485 lines (1291 loc) · 51.5 KB
/
preso.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Object oriented lib to Open Office Presentations
Copyright 2008-2009 Matt Harrison
Licensed under Apache License, Version 2.0 (current)
"""
import copy
import cStringIO as sio
import xml.etree.ElementTree as et
from xml.dom import minidom
import os
import sys
import tempfile
try:
import pygments
from pygments import formatter, lexers
pygmentsAvail = True
except:
print 'Could not import pygments code highlighting will not work'
pygmentsAvail = False
import zipwrap
import Image
import imagescale
DOC_CONTENT_ATTRIB = {
'office:version': '1.0',
'xmlns:anim':'urn:oasis:names:tc:opendocument:xmlns:animation:1.0',
'xmlns:chart': 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0',
'xmlns:dc': 'http://purl.org/dc/elements/1.1/',
'xmlns:dom': 'http://www.w3.org/2001/xml-events',
'xmlns:dr3d': 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0',
'xmlns:draw': 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0',
'xmlns:fo': 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0',
'xmlns:form': 'urn:oasis:names:tc:opendocument:xmlns:form:1.0',
'xmlns:math': 'http://www.w3.org/1998/Math/MathML',
'xmlns:meta': 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0',
'xmlns:number': 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0',
'xmlns:office': 'urn:oasis:names:tc:opendocument:xmlns:office:1.0',
'xmlns:presentation': 'urn:oasis:names:tc:opendocument:xmlns:presentation:1.0',
'xmlns:ooo': 'http://openoffice.org/2004/office',
'xmlns:oooc': 'http://openoffice.org/2004/calc',
'xmlns:ooow': 'http://openoffice.org/2004/writer',
'xmlns:script': 'urn:oasis:names:tc:opendocument:xmlns:script:1.0',
'xmlns:smil':'urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0',
'xmlns:style': 'urn:oasis:names:tc:opendocument:xmlns:style:1.0',
'xmlns:svg': 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0',
'xmlns:table': 'urn:oasis:names:tc:opendocument:xmlns:table:1.0',
'xmlns:text': 'urn:oasis:names:tc:opendocument:xmlns:text:1.0',
'xmlns:xforms': 'http://www.w3.org/2002/xforms',
'xmlns:xlink': 'http://www.w3.org/1999/xlink',
'xmlns:xsd': 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
}
NS2PREFIX = {}
for key, value in DOC_CONTENT_ATTRIB.items():
NS2PREFIX[value] = key.split(':')[-1]
TEXT_COUNT = 100
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
MONO_FONT = 'Courier New' # I like 'Envy Code R'
NORMAL_FONT = 'Arial'
SLIDE_WIDTH = 30 # cm
SLIDE_HEIGHT = 21
def cwd_decorator(func):
"""
decorator to change cwd to directory containing rst for this function
"""
def wrapper(*args, **kw):
cur_dir = os.getcwd()
found = False
for arg in sys.argv:
if arg.endswith(".rst"):
found = arg
break
if found:
directory = os.path.dirname(arg)
if directory:
os.chdir(directory)
data = func(*args, **kw)
os.chdir(cur_dir)
return data
return wrapper
class PrefixedWriter(et.ElementTree):
""" hacked to pass NS2PREFIX to _write """
def write(self, file, encoding="us-ascii"):
assert self._root is not None
if not hasattr(file, "write"):
file = open(file, "wb")
if not encoding:
encoding = "us-ascii"
elif encoding != "utf-8" and encoding != "us-ascii":
file.write("<?xml version='1.0' encoding='%s'?>\n" % encoding)
self._write(file, self._root, encoding, NS2PREFIX)
#self._write(file, self._root, encoding, {})
# Wrap etree elements to add parent attribute
def el(tag, attrib=None):
attrib = attrib or {}
el = et.Element(tag, attrib)
el.parent = None
return el
def sub_el(parent, tag, attrib=None):
attrib = attrib or {}
el = et.SubElement(parent, tag, attrib)
el.parent = parent
return el
def to_xml(node):
""" convert an etree node to xml """
fout = sio.StringIO()
#etree = PrefixedWriter(node)
etree = et.ElementTree(node)
etree.write(fout)
xml = fout.getvalue()
return xml
def pretty_xml(string_input, add_ns=False):
""" pretty indent string_input """
if add_ns:
elem = '<foo '
for key, value in DOC_CONTENT_ATTRIB.items():
elem += ' %s="%s"' %(key, value)
string_input = elem + '>' + string_input + '</foo>'
doc = minidom.parseString(string_input)
if add_ns:
s1 = doc.childNodes[0].childNodes[0].toprettyxml(' ')
else:
s1 = doc.toprettyxml(' ')
return s1
class Preso(object):
mime_type = 'application/vnd.oasis.opendocument.presentation'
def __init__(self):
self.slides = []
self.limit_pages = [] # can be list of page numbers (not indexes to export)
self._pictures = [] # list of Picture instances
self._footer_count = 0
# xml elements
self._root = None
self._auto_styles = None
self._presentation = None
self._styles_added = {}
self._init_xml()
def _init_xml(self):
self._root = el('office:document-content', attrib=DOC_CONTENT_ATTRIB)
o_scripts = sub_el(self._root, 'office:scripts')
self._auto_styles = sub_el(self._root, 'office:automatic-styles')
o_body = sub_el(self._root, 'office:body')
self._presentation = sub_el(o_body, 'office:presentation')
def add_imported_auto_style(self, style_node):
self._auto_styles.append(style_node)
style_node.parent = self._auto_styles
def import_slide(self, preso_file, page_num):
odp = zipwrap.ZipWrap(preso_file)
content = odp.cat('content.xml')
content_tree = et.fromstring(content)
slides = content_tree.findall('{urn:oasis:names:tc:opendocument:xmlns:office:1.0}body/{urn:oasis:names:tc:opendocument:xmlns:office:1.0}presentation/{urn:oasis:names:tc:opendocument:xmlns:drawing:1.0}page')
try:
slide_xml = slides[page_num - 1]
except IndexError, e:
print "Can't find page_num %d only %d slides" %(page_num, len(slides))
raise
if slide_xml:
self.slides.append(XMLSlide(self, slide_xml, odp))
def get_data(self, style_file=None):
if style_file and not os.path.exists(style_file):
print "template file %s doesn't exist" % style_file
assert False
fd, filename = tempfile.mkstemp()
zip_odp = self.to_file()
if style_file:
self.add_otp_style(zip_odp, style_file)
zip_odp.zipit(filename)
data = open(filename).read()
os.close(fd)
os.remove(filename)
return data
def add_otp_style(self, zip_odp, style_file):
"""
takes the slide content and merges in the style_file
"""
style = zipwrap.ZipWrap(style_file)
if 'Pictures' in style.cat(''):
for p in style.cat('Pictures'):
picture_file = 'Pictures/'+p
zip_odp.touch(picture_file, style.cat(picture_file))
zip_odp.touch('styles.xml', style.cat('styles.xml'))
return zip_odp
def to_file(self, filename=None):
"""
>>> p = Preso()
>>> z = p.to_file('/tmp/foo.odp')
>>> z.cat('/')
['settings.xml', 'META-INF', 'styles.xml', 'meta.xml', 'content.xml', 'mimetype']
"""
out = zipwrap.ZipWrap('')
out.touch('mimetype', self.mime_type)
for p in self._pictures:
out.touch('Pictures/%s' % p.internal_name, p.get_data())
out.touch('content.xml', self.to_xml())
out.touch('styles.xml', self.styles_xml())
out.touch('meta.xml', self.meta_xml())
out.touch('settings.xml', self.settings_xml())
out.touch('META-INF/manifest.xml', self.manifest_xml(out))
if filename:
out.zipit(filename)
return out
def manifest_xml(self, zip):
content = """<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
<manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.presentation" manifest:full-path="/"/>
"""
files = zip.cat('/')
try:
files.extend(zip.cat('/Pictures'))
except IOError, e:
# it's ok to not have pictures ;)
pass
for filename in files:
filetype = ''
if filename.endswith('.xml'):
filetype = 'text/xml'
elif filename.endswith('.jpg'):
filetype = 'image/jpeg'
elif filename.endswith('.gif'):
filetype = 'image/gif'
elif filename.endswith('.png'):
filetype = 'image/png'
elif filename == 'Configurations2/':
filetype = 'application/vnd.sun.xml.ui.configuration'
content += """ <manifest:file-entry manifest:media-type="%s" manifest:full-path="%s"/> """ % (filetype, filename)
content += """</manifest:manifest>"""
return content
def meta_xml(self):
return """<?xml version="1.0" encoding="UTF-8"?>
<office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:smil="urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0" xmlns:anim="urn:oasis:names:tc:opendocument:xmlns:animation:1.0" office:version="1.1">
<office:meta>
<meta:generator>odplib(python)</meta:generator>
<meta:creation-date>2008-09-15T11:12:02</meta:creation-date>
<dc:date>2008-10-01T20:32:43</dc:date>
<meta:editing-cycles>3</meta:editing-cycles>
<meta:editing-duration>PT26M35S</meta:editing-duration>
<meta:user-defined meta:name="Info 1"/>
<meta:user-defined meta:name="Info 2"/>
<meta:user-defined meta:name="Info 3"/>
<meta:user-defined meta:name="Info 4"/>
<meta:document-statistic meta:object-count="37"/>
</office:meta>
</office:document-meta>
"""
def settings_xml(self):
filename = os.path.join(DATA_DIR, 'settings.xml')
return open(filename).read()
def styles_xml(self):
filename = os.path.join(DATA_DIR, 'styles.xml')
data = open(filename).read()
if NORMAL_FONT != 'Arial':
data = data.replace('fo:font-family="Arial"',
'fo:font-family="%s"' %NORMAL_FONT)
return data
def to_xml(self):
for i, slide in enumerate(self.slides):
if self.limit_pages and i+1 not in self.limit_pages:
continue
if slide.footer:
footer_node = slide.footer.get_node()
self._presentation.append(footer_node)
footer_node.parent = self._presentation
node = slide.get_node()
self._presentation.append(node)
node.parent = self._presentation
return to_xml(self._root)
def add_style(self, style):
name = style.name
node = style.style_node()
if name not in self._styles_added:
self._styles_added[name] = 1
self._auto_styles.append(node)
def add_slide(self):
pnum = len(self.slides)+1
s = Slide(self, page_number=pnum)
self.slides.append(s)
return s
def copy_slide(self, s):
new_s = s._copy()
self.slides.append(new_s)
return new_s
def add_footer(self, f):
f.name = 'ftr%d'%(self._footer_count)
self._footer_count += 1
self.slides[-1].footer = f
class Animation(object):
ANIM_COUNT = 1
def __init__(self):
self.id = self._get_id()
def _get_id(self):
my_id = "id%d" % self.__class__.ANIM_COUNT
self.__class__.ANIM_COUNT += 1
return my_id
def get_node(self):
"""
<anim:par smil:begin="next">
<anim:par smil:begin="0s">
<anim:par smil:begin="0s" smil:fill="hold" presentation:node-type="on-click" presentation:preset-class="entrance" presentation:preset-id="ooo-entrance-appear">
<anim:set smil:begin="0s" smil:dur="0.001s" smil:fill="hold" smil:targetElement="id1" anim:sub-item="text" smil:attributeName="visibility" smil:to="visible"/>
</anim:par>
</anim:par>
</anim:par>
"""
par = el('anim:par', attrib={'smil:begin':'next'})
par2 = sub_el(par, 'anim:par', attrib={'smil:begin':'0s'})
par3 = sub_el(par2, 'anim:par', attrib={'smil:begin':'0s',
'smil:fill':'hold',
'presentation:node-type':'on-click',
'presentation:preset-class':'entrance',
'presentation:preset-id':'ooo-entrance-appear'})
anim_set = sub_el(par3, 'anim:set', attrib={'smil:begin':'0s',
'smil:dur':'0.001s',
'smil:fill':'hold',
'smil:targetElement':self.id,
'anim:sub-item':'text',
'smil:attributeName':'visibility',
'smil:to':'visible'})
return par
class ImportedPicture(object):
"""
Pictures used when importing slides
"""
def __init__(self, name, data):
self.internal_name = name
self.data = data
def get_data(self):
return self.data
class Picture(object):
"""
Need to convert to use image scale::
im = imagescale.ImageScale(uri)
x, y, w, h = im.adjust_size(WIDTH, HEIGHT)
x_str = "%fcm" % x
y_str = "%fcm" % y
w_str = "%fcm" % w
h_str = "%fcm" % h
frame = self._create_frame(attrib={ "draw:style-name":style_name,
"draw:text-style-name":"P6",
"draw:layer":"layout",
"svg:width":w_str, #"31.585cm",
"svg:height":h_str, #"21cm",
"svg:x":x_str, #"-1.781cm",
"svg:y":y_str #"0cm"
})
"""
COUNT = 0
CM_SCALE = 30.
def __init__(self, filepath, **kw):
self.filepath = filepath
image = Image.open(filepath)
self.w, self.h = image.size
self.internal_name = self._gen_name()
self.user_defined = {}
self._process_kw(kw)
def update_frame_attributes(self, attrib):
""" For positioning update the frame """
if 'align' in self.user_defined:
align = self.user_defined['align']
if 'top' in align:
attrib['style:vertical-pos'] = 'top'
if 'right' in align:
attrib['style:horizontal-pos'] = 'right'
return attrib
def _process_kw(self, kw):
self.user_defined = kw
def _gen_name(self):
ext = os.path.splitext(self.filepath)[1]
name = str(Picture.COUNT) + ext
Picture.COUNT += 1
return name
def get_xywh(self, measurement=None):
if measurement is None or measurement == 'cm':
measurement = 'cm'
scale = Picture.CM_SCALE
DPCM = 1 # dots per cm
if 'crop' in self.user_defined.get('classes', []):
x,y,w,h = imagescale.adjust_crop(SLIDE_WIDTH*DPCM, SLIDE_HEIGHT*DPCM,self.w, self.h)
elif 'fit' in self.user_defined.get('classes', []):
x,y,w,h = imagescale.adjust_fit(SLIDE_WIDTH*DPCM, SLIDE_HEIGHT*DPCM,self.w, self.h)
elif 'fill' in self.user_defined.get('classes', []):
x,y,w,h = 0,0,SLIDE_WIDTH,SLIDE_HEIGHT
else:
x,y,w,h = 1.4, 4.6, self.get_width(), self.get_height()
return [str(foo)+measurement for foo in [x,y,w,h]]
def get_width(self, measurement=None):
if measurement is None or measurement == 'cm':
measurement = 'cm'
scale = Picture.CM_SCALE
if 'width' in self.user_defined:
return self.user_defined['width'] + 'pt'
if 'scale' in self.user_defined:
return '%spt' % (self.w * float(self.user_defined['scale'])/100)
return str(self.w/scale)
def get_height(self, measurement=None):
if measurement is None:
measurement = 'cm'
if measurement == 'cm':
scale = Picture.CM_SCALE
if 'height' in self.user_defined:
return self.user_defined['height'] + 'pt'
if 'scale' in self.user_defined:
return '%spt' % (self.h * float(self.user_defined['scale'])/100)
return str(self.h/scale)
def get_data(self):
return open(self.filepath).read()
class XMLSlide(object):
PREFIX = 'IMPORT%d-%s'
COUNT = 0
def __init__(self, preso, node, odp_zipwrap):
self.preso = preso
self.page_node = node
self.footer = None
self.mangled = self._mangle_name()
self._init(odp_zipwrap)
def page_num(self):
""" not an int, usually 'Slide 1' or 'page1' """
name = self.page_node.attrib.get('{urn:oasis:names:tc:opendocument:xmlns:drawing:1.0}name', None)
return name
def _mangle_name(self):
name = self.PREFIX%(self.COUNT, self.page_num())
self.COUNT += 1
return name
def _init(self, odp_zipwrap):
# pull pictures out of slide
images = self.page_node.findall('*/{urn:oasis:names:tc:opendocument:xmlns:drawing:1.0}image')
for image in images:
path = image.attrib.get('{http://www.w3.org/1999/xlink}href')
data = odp_zipwrap.cat(path)
name = path.split('/')[1]
self.preso._pictures.append(ImportedPicture(name, data))
# pull styles out of content.xml (draw:style-name, draw:text-style-name, text:style-name)
styles_to_copy = {} #map of (attr_name, value) to value
attr_names = ['{urn:oasis:names:tc:opendocument:xmlns:drawing:1.0}style-name',
'{urn:oasis:names:tc:opendocument:xmlns:drawing:1.0}text-style-name',
'{urn:oasis:names:tc:opendocument:xmlns:text:1.0}style-name']
for node in self.page_node.getiterator():
for attr_name in attr_names:
style = node.attrib.get(attr_name, None)
if style:
styles_to_copy[style] = attr_name
# mangle name
node.attrib[attr_name] = self.mangled + style
auto_attr_names = ['{urn:oasis:names:tc:opendocument:xmlns:style:1.0}name']
found = {}
# get content.xml automatic-styles
content = odp_zipwrap.cat('content.xml')
content_node = et.fromstring(content)
auto_node = content_node.findall('{urn:oasis:names:tc:opendocument:xmlns:office:1.0}automatic-styles')[0]
for node in auto_node.getchildren():
for attr_name in auto_attr_names:
attr_value = node.attrib.get(attr_name, None)
if attr_value in styles_to_copy:
found[attr_value] = 1
# mangle name
node.attrib[attr_name] = self.mangled + attr_value
self.preso.add_imported_auto_style(node)
def get_node(self):
return self.page_node
class Slide(object):
def __init__(self, preso, page_number=None):
self.title_frame = None
self.text_frames = []
self._cur_text_frame = None
self.pic_frame = None
self._preso = preso
self.footer_frame = None
self.notes_frame = None
self.page_number = page_number
self.bullet_list = None # current bullet list
self.footer = None
self.animations = []
self.paragraph_attribs = {} # used to mark id's for animations
self.page_number_listeners = [self]
self.pending_styles = []
self.element_stack = [] # allow us to push pop
self.cur_element = None # if we write it could be to title,
# text or notes (Subclass of
# MixedContent)
self.insert_line_break = 0
# xml elements
self._page = None
self._init_xml()
def insert_line_breaks(self):
"""
If you want to write out existing line breaks, but don't have content to write
call this
"""
if self.cur_element:
self.cur_element.line_break()
#self.cur_element.write('')
def start_animation(self, anim):
self.animations.append(anim)
self.paragraph_attribs['text:id'] = anim.id
def end_animation(self):
# jump out of text:p
self.parent_of('text:p')
if 'text:id' in self.paragraph_attribs:
del self.paragraph_attribs['text:id']
def push_pending_node(self, name, attr):
"""
pending nodes are for affecting type, such as wrapping content
with text:a to make a hyperlink. Anything in pending nodes
will be written before the actual text.
User needs to remember to pop out of it.
"""
if self.cur_element is None:
self.add_text_frame()
self.cur_element.pending_nodes.append((name,attr))
def push_style(self, style):
if self.cur_element is None:
self.add_text_frame()
self.pending_styles.append(style)
def pop_style(self):
popped = self.pending_styles.pop()
def add_code(self, code, language):
if self.cur_element is None:
self.add_text_frame()
style = ParagraphStyle(**{'fo:text-align':'start'})
self.push_style(style)
output = pygments.highlight(code, lexers.get_lexer_by_name(language, stripall=True),
OdtCodeFormatter(self.cur_element, self._preso))
self.pop_style()
self.pop_node()
def add_picture(self, p):
"""
needs to look like this (under draw:page)
<draw:frame draw:style-name="gr2" draw:text-style-name="P2" draw:layer="layout" svg:width="19.589cm" svg:height="13.402cm" svg:x="3.906cm" svg:y="4.378cm">
<draw:image xlink:href="Pictures/10000201000002F800000208188B22AE.png" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad">
<text:p text:style-name="P1"/>
</draw:image>
</draw:frame>
"""
# pictures should be added the the draw:frame element
self.pic_frame = PictureFrame(self, p)
self.pic_frame.add_node('draw:image', attrib={'xlink:href': 'Pictures/' + p.internal_name,
'xlink:type':'simple',
'xlink:show':'embed',
'xlink:actuate':'onLoad' })
self._preso._pictures.append(p)
node = self.pic_frame.get_node()
self._page.append(node)
node.parent = self._page
def push_element(self):
""" element push/pop is used to remember previous cur_elem, since
lists might need to mess with that"""
self.element_stack.append(self.cur_element)
def pop_element(self):
self.cur_element = self.element_stack.pop()
def to_xml(self):
node = self.get_node()
return to_xml(node)
def _fire_page_number(self, new_num):
for listener in self.page_number_listeners:
listener.new_page_num(new_num)
def new_page_num(self, new_num):
self._page.attrib['draw:name'] = 'page%d' % self.page_number
def _copy(self):
''' needs to update page numbers '''
ins = copy.copy(self)
ins._fire_page_number(self.page_number+1)
return ins
def _init_xml(self):
self._page = el('draw:page', attrib={
'draw:name':'page%d' % self.page_number,
'draw:style-name':'dp1',
'draw:master-page-name':'Default',
'presentation:presentation-page-layout-name':'AL1T0'
})
office_forms = sub_el(self._page, 'office:forms',
attrib={'form:automatic-focus':'false',
'form:apply-design-mode':'false'})
def get_node(self):
"""return etree Element representing this slide"""
# already added title, text frames
# add animation chunks
if self.animations:
anim_par = el('anim:par', attrib={'presentation:node-type':'timing-root'})
self._page.append(anim_par)
anim_par.parent = self._page
anim_seq = sub_el(anim_par, 'anim:seq', attrib={'presentation:node-type':'main-sequence'})
for a in self.animations:
a_node = a.get_node()
anim_seq.append(a_node)
a_node.parent = anim_seq
# add notes now (so they are last)
if self.notes_frame:
notes = self.notes_frame.get_node()
self._page.append(notes)
notes.parent = self._page
if self.footer:
self._page.attrib['presentation:use-footer-name'] = self.footer.name
return self._page
def add_text_frame(self, attrib=None):
# should adjust width, x based on if existing boxes
self.text_frames.append(TextFrame(self, attrib))
node = self.text_frames[-1].get_node()
self._page.append(node)
node.parent = self._page
self.cur_element = self.text_frames[-1]
return self.text_frames[-1]
def add_title_frame(self):
self.title_frame = TitleFrame(self)
node = self.title_frame.get_node()
self._page.append(node)
node.parent = self._page
self.cur_element = self.title_frame
return self.title_frame
def add_notes_frame(self):
self.notes_frame = NotesFrame(self)
self.page_number_listeners.append(self.notes_frame)
self.cur_element = self.notes_frame
return self.notes_frame
def add_list(self, bl):
"""
note that this pushes the cur_element, but doesn't pop it.
You'll need to do that
"""
# text:list doesn't like being a child of text:p
if self.cur_element is None:
self.add_text_frame()
self.push_element()
self.cur_element._text_box.append(bl.node)
bl.node.parent = self.cur_element._text_box
style = bl.style_name
if style not in self._preso._styles_added:
self._preso._styles_added[style] = 1
self._preso._auto_styles.append(et.fromstring(bl.default_styles())[0])
self.cur_element = bl
def add_table(self, t):
"""
remember to call pop_element after done with table
"""
self.push_element()
self._page.append(t.node)
t.node.parent = self._page
self.cur_element = t
def write(self, text, **kw):
if self.cur_element is None:
self.add_text_frame()
self.cur_element.write(text, **kw)
def add_node(self, node, attrib=None):
attrib = attrib or {}
if self.cur_element is None:
self.add_text_frame()
self.cur_element.add_node(node, attrib)
def pop_node(self):
self.cur_element.pop_node()
def parent_of(self, name):
"""
like pop_node, but traverse up parents. When you find a node
with name, set cur_node to that
"""
if self.cur_element:
self.cur_element.parent_of(name)
class MixedContent(object):
def __init__(self, slide, name, attrib=None):
self._default_align = 'start'
self.slide = slide
if attrib is None:
attrib = {}
self.node = el(name, attrib)
self.cur_node = self.node
# store nodes that affect output (such as text:a)
self.pending_nodes = [] # typles of (name, attr)
self.dirty = False # keep track if we have been written to
def parent_of(self, name):
"""
go to parent of node with name, and set as cur_node. Useful
for creating new paragraphs
"""
if not self._in_tag(name):
return
node = self.cur_node
while node.tag != name:
node = node.parent
self.cur_node = node.parent
def _in_p(self):
"""
Determine if we are already in a text:p, odp doesn't like
nested ones too much
"""
return self._in_tag('text:p')
def _is_last_child(self, tagname, attributes=None):
"""
Check if last child of cur_node is tagname with attributes
"""
children = self.cur_node.getchildren()
if children:
result = self._is_node(tagname, attributes, node=children[-1])
return result
return False
def _is_node(self, tagname, attributes=None, node=None):
if node is None:
node = self.cur_node
if attributes:
return node.tag == tagname and node.attrib == attributes
else:
return node.tag == tagname
def _in_tag(self, tagname, attributes=None):
"""
Determine if we are already in a certain tag.
If we give attributes, make sure they match.
"""
node = self.cur_node
while not node is None:
if node.tag == tagname:
if attributes and node.attrib == attributes:
return True
elif attributes:
return False
return True
node = node.parent
return False
def to_xml(self):
return to_xml(self.node)
def get_node(self):
return self.node
def append(self, node):
self.cur_node.append(node)
node.parent = self.cur_node
def _check_add_node(self, parent, name):
''' Returns False if bad to make name a child of parent '''
if name == 'text:a':
if parent.tag == 'draw:text-box':
return False
return True
def _add_node(self, parent, name, attrib):
if not self._check_add_node(parent, name):
raise Exception, 'Bad child (%s) for %s)' %(name, parent.tag)
new_node = sub_el(parent, name, attrib)
return new_node
def add_node(self, node_name, attrib=None):
if attrib is None:
attrib = {}
new_node = self._add_node(self.cur_node, node_name, attrib)
self.cur_node = new_node
return self.cur_node
def pop_node(self):
if self.cur_node.parent == self.node:
# Don't pop too far !!
return
if self.cur_node.parent is None:
return
self.cur_node = self.cur_node.parent
def _add_styles(self, add_paragraph=True, add_text=True):
"""
Adds paragraph and span wrappers if necessary based on style
"""
p_styles = {'fo:text-align':self._default_align}
t_styles = {}
for s in self.slide.pending_styles:
if isinstance(s, ParagraphStyle):
p_styles.update(s.styles)
elif isinstance(s, TextStyle):
t_styles.update(s.styles)
para = ParagraphStyle(**p_styles)
if add_paragraph or self.slide.paragraph_attribs:
p_attrib = {'text:style-name':para.name}
p_attrib.update(self.slide.paragraph_attribs)
# if self._is_last_child('text:p', p_attrib):
# children = self.cur_node.getchildren()
# self.cur_node = children[-1]
#elif not self._in_tag('text:p', p_attrib):
if not self._in_tag('text:p', p_attrib):
self.parent_of('text:p')
# Create paragraph style first
self.slide._preso.add_style(para)
self.add_node('text:p', attrib=p_attrib)
# span is only necessary if style changes
if add_text and t_styles:
text = TextStyle(**t_styles)
children = self.cur_node.getchildren()
if children:
# if we already are using this text style, reuse the last one
last = children[-1]
if last.tag == 'text:span' and \
last.attrib['text:style-name'] == text.name and \
last.tail is None: # if we have a tail, we can't reuse
self.cur_node = children[-1]
return
if not self._is_node('text:span', {'text:style-name':text.name}):
# Create text style
self.slide._preso.add_style(text)
self.add_node('text:span', attrib={'text:style-name':text.name})
def _add_pending_nodes(self):
for node, attr in self.pending_nodes:
self.add_node(node, attr)
def line_break(self):
"""insert as many line breaks as the insert_line_break variable says
"""
for i in range(self.slide.insert_line_break):
# needs to be inside text:p
if not self._in_tag('text:p'):
# we can just add a text:p and no line-break
# Create paragraph style first
self.add_node('text:p')
#else:
self.add_node('text:line-break')
self.pop_node()
if self.cur_node.tag == 'text:p':
return
if self.cur_node.parent.tag != 'text:p':
self.pop_node()
self.slide.insert_line_break = 0
def write(self, text, add_p_style=True, add_t_style=True):
"""
see mixed content
http://effbot.org/zone/element-infoset.htm#mixed-content
Writing is complicated by requirements of odp to ignore
duplicate spaces together. Deal with this by splitting on
white spaces then dealing with the '' (empty strings) which
would be the extra spaces
"""
self._add_styles(add_p_style, add_t_style)
#self.line_break()
self._add_pending_nodes()
spaces = []
for i, letter in enumerate(text):
if letter == ' ':
spaces.append(letter)
continue
elif len(spaces) == 1:
self._write(' ')
self._write(letter)
spaces = []
continue
elif spaces:
num_spaces = len(spaces) - 1
# write just a plain space at the start
self._write(' ')
if num_spaces > 1:
# write the attrib only if more than one space
self.add_node('text:s', {'text:c':str(num_spaces)})
else:
self.add_node('text:s')
self.pop_node()
self._write(letter)
spaces = []
continue
self._write(letter)
# might have dangling spaces
# if len(spaces) == 1:
# self._write(' ')
#elif spaces:
if spaces:
num_spaces = len(spaces)
##num_spaces = len(spaces) - 1
# write space
##self._write(' ')
if num_spaces > 1:
self.add_node('text:s', {'text:c':str(num_spaces)})
else:
self.add_node('text:s')
self.pop_node()
def _write(self, letter):
children = self.cur_node.getchildren()
if children:
child = children[-1]
cur_text = child.tail or ''