1#!/usr/bin/env python
2
3import sys
4sys.path.append( '../' )
5
6from PyRTF import *
7
8SAMPLE_PARA = """The play opens one year after the death of Richard II, and
9King Henry is making plans for a crusade to the Holy Land to cleanse himself
10of the guilt he feels over the usurpation of Richard's crown. But the crusade
11must be postponed when Henry learns that Welsh rebels, led by Owen Glendower,
12have defeated and captured Mortimer. Although the brave Henry Percy, nicknamed
13Hotspur, has quashed much of the uprising, there is still much trouble in
14Scotland. King Henry has a deep admiration for Hotspur and he longs for his
15own son, Prince Hal, to display some of Hotspur's noble qualities. Hal is more
16comfortable in a tavern than on the battlefield, and he spends his days
17carousing with riff-raff in London. But King Henry also has his problems with
18the headstrong Hotspur, who refuses to turn over his prisoners to the state as
19he has been so ordered. Westmoreland tells King Henry that Hotspur has many of
20the traits of his uncle, Thomas Percy, the Earl of Worcester, and defying
21authority runs in the family."""
22
23def MakeExample1() :
24	doc     = Document()
25	ss      = doc.StyleSheet
26	section = Section()
27	doc.Sections.append( section )
28
29	#	text can be added directly to the section
30	#	a paragraph object is create as needed
31	section.append( 'Example 1' )
32
33	#	blank paragraphs are just empty strings
34	section.append( '' )
35
36	#	a lot of useful documents can be created
37	#	with little more than this
38	section.append( 'A lot of useful documents can be created '
39					'in this way, more advance formating is available '
40					'but a lot of users just want to see their data come out '
41					'in something other than a text file.' )
42	return doc
43
44def MakeExample2() :
45	doc     = Document()
46	ss      = doc.StyleSheet
47	section = Section()
48	doc.Sections.append( section )
49
50	#	things really only get interesting after you
51	#	start to use styles to spice things up
52	p = Paragraph( ss.ParagraphStyles.Heading1 )
53	p.append( 'Example 2' )
54	section.append( p )
55
56	p = Paragraph( ss.ParagraphStyles.Normal )
57	p.append( 'In this case we have used a two styles. '
58			  'The first paragraph is marked with the Heading1 style so that it '
59			  'will appear differently to the user. ')
60	section.append( p )
61
62	p = Paragraph()
63	p.append( 'Notice that after I have changed the style of the paragraph '
64			  'all subsequent paragraphs have that style automatically. '
65			  'This saves typing and is the default behaviour for RTF documents.' )
66	section.append( p )
67
68	p = Paragraph()
69	p.append( 'I also happen to like Arial so our base style is Arial not Times New Roman.' )
70	section.append( p )
71
72	p = Paragraph()
73	p.append( 'It is also possible to provide overrides for element of a style. ',
74			  'For example I can change just the font ',
75			  TEXT( 'size', size=48 ),
76			  ' or ',
77			  TEXT( 'typeface', font=ss.Fonts.Impact ) ,
78			  '.' )
79	section.append( p )
80
81	p = Paragraph()
82	p.append( 'The paragraph itself can also be overridden in lots of ways, tabs, '
83			  'borders, alignment, etc can all be modified either in the style or as an '
84			  'override during the creation of the paragraph. '
85			  'The next paragraph demonstrates custom tab widths and embedded '
86			  'carriage returns, ie new line markers that do not cause a paragraph break.' )
87	section.append( p )
88
89	#	ParagraphPS is an alias for ParagraphPropertySet
90	para_props = ParagraphPS( tabs = [ TabPS( width=TabPS.DEFAULT_WIDTH     ),
91									   TabPS( width=TabPS.DEFAULT_WIDTH * 2 ),
92									   TabPS( width=TabPS.DEFAULT_WIDTH     ) ] )
93	p = Paragraph( ss.ParagraphStyles.Normal, para_props )
94	p.append( 'Left Word', TAB, 'Middle Word', TAB, 'Right Word', LINE,
95			  'Left Word', TAB, 'Middle Word', TAB, 'Right Word' )
96	section.append( p )
97
98	section.append( 'The alignment of tabs and style can also be controlled. '
99					'The following paragraph demonstrates how to use flush right tabs'
100					'and leader dots.' )
101
102	para_props = ParagraphPS( tabs = [ TabPS( section.TwipsToRightMargin(),
103											  alignment = TabPS.RIGHT,
104											  leader    = TabPS.DOTS  ) ] )
105	p = Paragraph( ss.ParagraphStyles.Normal, para_props )
106	p.append( 'Before Dots', TAB, 'After Dots' )
107	section.append( p )
108
109	section.append( 'Paragraphs can also be indented, the following is all at the '
110					'same indent level and the one after it has the first line '
111					'at a different indent to the rest.  The third has the '
112					'first line going in the other direction and is also separated '
113					'by a page break.  Note that the '
114					'FirstLineIndent is defined as being the difference from the LeftIndent.' )
115
116	section.append( 'The following text was copied from http://www.shakespeare-online.com/plots/1kh4ps.html.' )
117
118	para_props = ParagraphPS()
119	para_props.SetLeftIndent( TabPropertySet.DEFAULT_WIDTH *  3 )
120	p = Paragraph( ss.ParagraphStyles.Normal, para_props )
121	p.append( SAMPLE_PARA )
122	section.append( p )
123
124	para_props = ParagraphPS()
125	para_props.SetFirstLineIndent( TabPropertySet.DEFAULT_WIDTH * -2 )
126	para_props.SetLeftIndent( TabPropertySet.DEFAULT_WIDTH *  3 )
127	p = Paragraph( ss.ParagraphStyles.Normal, para_props )
128	p.append( SAMPLE_PARA )
129	section.append( p )
130
131	#	do a page
132	para_props = ParagraphPS()
133	para_props.SetPageBreakBefore( True )
134	para_props.SetFirstLineIndent( TabPropertySet.DEFAULT_WIDTH )
135	para_props.SetLeftIndent( TabPropertySet.DEFAULT_WIDTH )
136	p = Paragraph( ss.ParagraphStyles.Normal, para_props )
137	p.append( SAMPLE_PARA )
138	section.append( p )
139
140	return doc
141
142
143def MakeExample3() :
144	doc     = Document()
145	ss      = doc.StyleSheet
146	section = Section()
147	doc.Sections.append( section )
148
149	p = Paragraph( ss.ParagraphStyles.Heading1 )
150	p.append( 'Example 3' )
151	section.append( p )
152
153	# changes what is now the default style of Heading1 back to Normal
154	p = Paragraph( ss.ParagraphStyles.Normal )
155	p.append( 'Example 3 demonstrates tables, tables represent one of the '
156			  'harder things to control in RTF as they offer alot of '
157			  'flexibility in formatting and layout.' )
158	section.append( p )
159
160	section.append( 'Table columns are specified in widths, the following example '
161					'consists of a table with 3 columns, the first column is '
162					'7 tab widths wide, the next two are 3 tab widths wide. '
163					'The widths chosen are arbitrary, they do not have to be '
164					'multiples of tab widths.' )
165
166	table = Table( TabPS.DEFAULT_WIDTH * 7,
167				   TabPS.DEFAULT_WIDTH * 3,
168				   TabPS.DEFAULT_WIDTH * 3 )
169	c1 = Cell( Paragraph( 'Row One, Cell One'   ) )
170	c2 = Cell( Paragraph( 'Row One, Cell Two'   ) )
171	c3 = Cell( Paragraph( 'Row One, Cell Three' ) )
172	table.AddRow( c1, c2, c3 )
173
174	c1 = Cell( Paragraph( ss.ParagraphStyles.Heading2, 'Heading2 Style'   ) )
175	c2 = Cell( Paragraph( ss.ParagraphStyles.Normal, 'Back to Normal Style'   ) )
176	c3 = Cell( Paragraph( 'More Normal Style' ) )
177	table.AddRow( c1, c2, c3 )
178
179	c1 = Cell( Paragraph( ss.ParagraphStyles.Heading2, 'Heading2 Style'   ) )
180	c2 = Cell( Paragraph( ss.ParagraphStyles.Normal, 'Back to Normal Style'   ) )
181	c3 = Cell( Paragraph( 'More Normal Style' ) )
182	table.AddRow( c1, c2, c3 )
183
184	section.append( table )
185	section.append( 'Different frames can also be specified for each cell in the table '
186					'and each frame can have a different width and style for each border.' )
187
188	thin_edge  = BorderPS( width=20, style=BorderPS.SINGLE )
189	thick_edge = BorderPS( width=80, style=BorderPS.SINGLE )
190
191	thin_frame  = FramePS( thin_edge,  thin_edge,  thin_edge,  thin_edge )
192	thick_frame = FramePS( thick_edge, thick_edge, thick_edge, thick_edge )
193	mixed_frame = FramePS( thin_edge,  thick_edge, thin_edge,  thick_edge )
194
195	table = Table( TabPS.DEFAULT_WIDTH * 3, TabPS.DEFAULT_WIDTH * 3, TabPS.DEFAULT_WIDTH * 3 )
196	c1 = Cell( Paragraph( 'R1C1' ), thin_frame )
197	c2 = Cell( Paragraph( 'R1C2' ) )
198	c3 = Cell( Paragraph( 'R1C3' ), thick_frame )
199	table.AddRow( c1, c2, c3 )
200
201	c1 = Cell( Paragraph( 'R2C1' ) )
202	c2 = Cell( Paragraph( 'R2C2' ) )
203	c3 = Cell( Paragraph( 'R2C3' ) )
204	table.AddRow( c1, c2, c3 )
205
206	c1 = Cell( Paragraph( 'R3C1' ), mixed_frame )
207	c2 = Cell( Paragraph( 'R3C2' ) )
208	c3 = Cell( Paragraph( 'R3C3' ), mixed_frame )
209	table.AddRow( c1, c2, c3 )
210
211	section.append( table )
212
213	section.append( 'In fact frames can be applied to paragraphs too, not just cells.' )
214
215	p = Paragraph( ss.ParagraphStyles.Normal, thin_frame )
216	p.append( 'This whole paragraph is in a frame.' )
217	section.append( p )
218	return doc
219
220def MakeExample4() :
221	doc     = Document()
222	ss      = doc.StyleSheet
223	section = Section()
224	doc.Sections.append( section )
225
226	section.append( 'Example 4' )
227	section.append( 'This example test changing the colour of fonts.' )
228
229	#
230	#	text properties can be specified in two ways, either a
231	#	Text object can have its text properties specified like:
232	tps = TextPS( colour=ss.Colours.Red )
233	text = Text( 'RED', tps )
234	p = Paragraph()
235	p.append( 'This next word should be in ', text )
236	section.append( p )
237
238	#	or the shortcut TEXT function can be used like:
239	p = Paragraph()
240	p.append( 'This next word should be in ', TEXT( 'Green', colour=ss.Colours.Green ) )
241	section.append( p )
242
243	#	when specifying colours it is important to use the colours from the
244	#	style sheet supplied with the document and not the StandardColours object
245	#	each document get its own copy of the stylesheet so that changes can be
246	#	made on a document by document basis without mucking up other documents
247	#	that might be based on the same basic stylesheet
248
249	return doc
250
251
252#
253#  DO SOME WITH HEADERS AND FOOTERS
254def MakeExample5() :
255	doc     = Document()
256	ss      = doc.StyleSheet
257	section = Section()
258	doc.Sections.append( section )
259
260	section.Header.append( 'This is the header' )
261	section.Footer.append( 'This is the footer' )
262
263	p = Paragraph( ss.ParagraphStyles.Heading1 )
264	p.append( 'Example 5' )
265	section.append( p )
266
267	#	blank paragraphs are just empty strings
268	section.append( '' )
269
270	p = Paragraph( ss.ParagraphStyles.Normal )
271	p.append( 'This document has a header and a footer.' )
272	section.append( p )
273
274	return doc
275
276def MakeExample6() :
277	doc     = Document()
278	ss      = doc.StyleSheet
279	section = Section()
280	doc.Sections.append( section )
281
282	section.FirstHeader.append( 'This is the header for the first page.' )
283	section.FirstFooter.append( 'This is the footer for the first page.' )
284
285	section.Header.append( 'This is the header that will appear on subsequent pages.' )
286	section.Footer.append( 'This is the footer that will appear on subsequent pages.' )
287
288	p = Paragraph( ss.ParagraphStyles.Heading1 )
289	p.append( 'Example 6' )
290	section.append( p )
291
292	#	blank paragraphs are just empty strings
293	section.append( '' )
294
295	p = Paragraph( ss.ParagraphStyles.Normal )
296	p.append( 'This document has different headers and footers for the first and then subsequent pages. '
297			  'If you insert a page break you should see a different header and footer.' )
298	section.append( p )
299
300	return doc
301
302def MakeExample7() :
303	doc     = Document()
304	ss      = doc.StyleSheet
305	section = Section()
306	doc.Sections.append( section )
307
308	section.FirstHeader.append( 'This is the header for the first page.' )
309	section.FirstFooter.append( 'This is the footer for the first page.' )
310
311	section.Header.append( 'This is the header that will appear on subsequent pages.' )
312	section.Footer.append( 'This is the footer that will appear on subsequent pages.' )
313
314	p = Paragraph( ss.ParagraphStyles.Heading1 )
315	p.append( 'Example 7' )
316	section.append( p )
317
318	p = Paragraph( ss.ParagraphStyles.Normal )
319	p.append( 'This document has different headers and footers for the first and then subsequent pages. '
320			  'If you insert a page break you should see a different header and footer.' )
321	section.append( p )
322
323	p = Paragraph( ss.ParagraphStyles.Normal, ParagraphPS().SetPageBreakBefore( True ) )
324	p.append( 'This should be page 2 '
325			  'with the subsequent headers and footers.' )
326	section.append( p )
327
328	section = Section( break_type=Section.PAGE, first_page_number=1 )
329	doc.Sections.append( section )
330
331	section.FirstHeader.append( 'This is the header for the first page of the second section.' )
332	section.FirstFooter.append( 'This is the footer for the first page of the second section.' )
333
334	section.Header.append( 'This is the header that will appear on subsequent pages for the second section.' )
335	p = Paragraph( 'This is the footer that will appear on subsequent pages for the second section.', LINE )
336	p.append( PAGE_NUMBER, ' of ', SECTION_PAGES )
337	section.Footer.append( p )
338
339	section.append( 'This is the first page' )
340
341	p = Paragraph( ParagraphPS().SetPageBreakBefore( True ), 'This is the second page' )
342	section.append( p )
343
344	return doc
345
346def OpenFile( name ) :
347	return file( '%s.rtf' % name, 'w' )
348
349if __name__ == '__main__' :
350	DR = Renderer()
351
352	doc1 = MakeExample1()
353	doc2 = MakeExample2()
354	doc3 = MakeExample3()
355	doc4 = MakeExample4()
356	doc5 = MakeExample5()
357	doc6 = MakeExample6()
358	doc7 = MakeExample7()
359
360	DR.Write( doc1, OpenFile( '1' ) )
361	DR.Write( doc2, OpenFile( '2' ) )
362	DR.Write( doc3, OpenFile( '3' ) )
363	DR.Write( doc4, OpenFile( '4' ) )
364	DR.Write( doc5, OpenFile( '5' ) )
365	DR.Write( doc6, OpenFile( '6' ) )
366	DR.Write( doc7, OpenFile( '7' ) )
367
368	print "Finished"
369