|
Package parasol ::
Module PPT_wrapper
|
|
1 from win32com.client import Dispatch
2
3 msoAlignBottoms =0x5
4 msoAlignCenters =0x1
5 msoAlignLefts =0x0
6 msoAlignMiddles =0x4
7 msoAlignRights =0x2
8 msoAlignTops =0x3
9
10 msoAnchorBottom =0x4
11 msoAnchorBottomBaseLine =0x5
12 msoAnchorMiddle =0x3
13 msoAnchorTop =0x1
14 msoAnchorTopBaseline =0x2
15 msoVerticalAnchorMixed =-2
16
17 msoAnchorCenter =0x2
18 msoAnchorNone =0x1
19 msoHorizontalAnchorMixed =-2
20
21 ppBulletNone =0x0
22
23 ppLayoutBlank =0xc
24 ppLayoutChart =0x8
25 ppLayoutChartAndText =0x6
26 ppLayoutClipArtAndVerticalText=0x1a
27 ppLayoutClipartAndText =0xa
28 ppLayoutFourObjects =0x18
29 ppLayoutLargeObject =0xf
30 ppLayoutMediaClipAndText =0x12
31 ppLayoutMixed =-2
32 ppLayoutObject =0x10
33 ppLayoutObjectAndText =0xe
34 ppLayoutObjectAndTwoObjects =0x1e
35 ppLayoutObjectOverText =0x13
36 ppLayoutOrgchart =0x7
37 ppLayoutTable =0x4
38 ppLayoutText =0x2
39 ppLayoutTextAndChart =0x5
40 ppLayoutTextAndClipart =0x9
41 ppLayoutTextAndMediaClip =0x11
42 ppLayoutTextAndObject =0xd
43 ppLayoutTextAndTwoObjects =0x15
44 ppLayoutTextOverObject =0x14
45 ppLayoutTitle =0x1
46 ppLayoutTitleOnly =0xb
47 ppLayoutTwoColumnText =0x3
48 ppLayoutTwoObjects =0x1d
49 ppLayoutTwoObjectsAndObject =0x1f
50 ppLayoutTwoObjectsAndText =0x16
51 ppLayoutTwoObjectsOverText =0x17
52 ppLayoutVerticalText =0x19
53 ppLayoutVerticalTitleAndText =0x1b
54 ppLayoutVerticalTitleAndTextOverChart=0x1c
55
56 msoFalse =0x0
57 msoTrue =-1
58
59 ppAlignCenter =0x2
60 ppAlignDistribute =0x5
61 ppAlignJustify =0x4
62 ppAlignJustifyLow =0x7
63 ppAlignLeft =0x1
64 ppAlignRight =0x3
65 ppAlignThaiDistribute =0x6
66 ppAlignmentMixed =-2
67
69
71
72 self.PPTapp = Dispatch('Powerpoint.Application')
73 if templatefile == None:
74 self.ppt = self.PPTapp.Presentations.Add(1)
75 else:
76 self.ppt = self.PPTapp.Presentations.Open(\
77 FileName=templatefile, Untitled=msoTrue)
78 self.w = self.ppt.PageSetup.SlideWidth
79 self.h = self.ppt.PageSetup.SlideHeight
80
81 self.index = 0
82 self.slideL = []
83
84
86
87 self.PPTapp.Visible = 1
88
90 self.index += 1
91 Slide = self.ppt.Slides.Add(Index=self.index,
92 Layout=ppLayoutTable)
93 if self.PPTapp.Visible:
94 Slide.Select()
95
96 Slide.Shapes.Placeholders(1).TextFrame.TextRange.Text = title
97 Font = Slide.Shapes.Placeholders(1).TextFrame.TextRange.Font
98
99 Font.Size = '32'
100
101 self.slideL.append( Slide )
102
103
104
105 - def addTextSlide(self, text='my text', title='my title',
106 textFont='', textFontSize=None, center=1, wrap=0,
107 noBullets=0, makeItFit=1):
108
109 self.index += 1
110 Slide = self.ppt.Slides.Add(Index=self.index,
111 Layout=ppLayoutText)
112 if self.PPTapp.Visible:
113 Slide.Select()
114
115 self.slideL.append( Slide )
116
117 Slide.Shapes.Placeholders(1).TextFrame.TextRange.Text = title
118 Font = Slide.Shapes.Placeholders(1).TextFrame.TextRange.Font
119
120 Font.Size = '32'
121
122 textObj = Slide.Shapes.Placeholders(2)
123 textRng = Slide.Shapes.Placeholders(2).TextFrame.TextRange
124 FontTxt = textRng.Font
125 textFrame = Slide.Shapes.Placeholders(2).TextFrame
126
127
128 if noBullets:
129 textRng.ParagraphFormat.Bullet.Type = ppBulletNone
130
131 if textFont:
132 FontTxt.Name = textFont
133
134 textRng.Text = text
135
136
137
138 if textFontSize != None:
139 if int(textFontSize)>=8:
140 FontTxt.Size = str(textFontSize)
141
142
143
144
145
146
147
148 if makeItFit:
149
150 fontSize = int(FontTxt.Size)
151 w = textObj.Width
152 h = textObj.Height
153 titleObj = Slide.Shapes.Placeholders(1)
154 htit = titleObj.Top + titleObj.Height
155 print "w=",w,"window w=",self.w
156 print "h=",h,"window h=",self.h
157 if (w>self.w-40 or h>self.h-htit) and fontSize>8:
158 fontSize = fontSize - 2
159 FontTxt.Size = str(fontSize)
160 textObj.Width = textRng.BoundWidth
161 textObj.Height = textRng.BoundHeight
162 print "reduced PowerPoint Font to",fontSize
163 w = textObj.Width
164 h = textObj.Height
165 print "new width=",w,"new height=",h
166
167 if not wrap:
168 textFrame.WordWrap = msoFalse
169 if center:
170 textFrame.VerticalAnchor = msoAnchorMiddle
171 textFrame.HorizontalAnchor = msoAnchorCenter
172
174 if len(imgFile)<5:
175 return
176
177 self.index += 1
178 Slide = self.ppt.Slides.Add(Index=self.index,
179 Layout=ppLayoutTitleOnly)
180
181 self.slideL.append( Slide )
182
183 if self.PPTapp.Visible:
184 Slide.Select()
185
186
187
188
189 Slide.Shapes.Placeholders(1).TextFrame.TextRange.Text = title
190 Font = Slide.Shapes.Placeholders(1).TextFrame.TextRange.Font
191
192 Font.Size = '32'
193
194 Picture = Slide.Shapes.AddPicture(imgFile, LinkToFile=False,
195 SaveWithDocument=True,
196 Left=10, Top=12, Width=-1.0, Height=-1.0)
197
198
199 tp = Slide.Shapes.Placeholders(1).Top + \
200 Slide.Shapes.Placeholders(1).Height
201 hp = Picture.Height
202 wp = Picture.Width
203
204
205 Picture.Top = tp + 10
206 Picture.Left = max(1, (self.w-wp)/2)
207
208
210 self.ppt.Saved = False
211 self.ppt.SaveAs(filename)
212
214 self.ppt.Saved = True
215
218
219 if __name__ == "__main__":
220
221 p = PPTwrap()
222 p.show()
223
224 p.addImageSlide( imgFile=r'C:\my_gif\irobot2.gif',
225 title='Sensitivity Plot')
226
227 p.addImageSlide( imgFile=r'C:\my_gif\escher01.gif',
228 title='Delivered Isp')
229
230 pptText = 'Now is the time for all good charts to blossom\r' +\
231 'Now is the time for all good charts to blossom\r' +\
232 'Now good charts blossom\r' +\
233 'Now is the time for all good charts to blossom\r' +\
234 'Now is the time for all good charts to blossom\r' +\
235 'Now is the time for all all all alll good charts to blossom\r' +\
236 'Now good charts blossom\r' +\
237 'Now is the time for all good charts to blossom\r' +\
238 'Now is the time for all good charts to blossom\r'
239 p.addTextSlide(text=pptText, title='my title',
240 textFont='Courier New', textFontSize=18, center=1,
241 noBullets=1, makeItFit=1)
242 p.addTextSlide(text=pptText, title='my title',
243 textFont='Courier New', textFontSize=18, center=1,
244 noBullets=1, makeItFit=1, wrap=1)
245
246 p.abandon()
247
248
249
250