Advertisement
Guest User

Beginning Python #24 - PyRTF

a guest
May 9th, 2011
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from PyRTF import *
  4.  
  5. def MakeExample() :
  6.     doc = Document()
  7.     ss  = doc.StyleSheet
  8.     section = Section()
  9.     doc.Sections.append(section)
  10.  
  11.     result = doc.StyleSheet
  12.     NormalText = TextStyle(TextPropertySet(result.Fonts.CourierNew,16))
  13.     ps2 = ParagraphStyle('Courier',NormalText.Copy())
  14.     result.ParagraphStyles.append(ps2)
  15.    
  16.     # And one final one for 11 pt Arial Bold in Red
  17.     NormalText = TextStyle(TextPropertySet(result.Fonts.Arial,22,bold=True,colour=ss.Colours.Red))
  18.     ps2 = ParagraphStyle('ArialBoldRed',NormalText.Copy())
  19.     result.ParagraphStyles.append(ps2)    
  20.  
  21.  
  22.     p = Paragraph(ss.ParagraphStyles.Heading1)  
  23.     p.append('Example Heading 1')
  24.     section.append(p)
  25.  
  26.     p = Paragraph(ss.ParagraphStyles.Normal)
  27.     p.append('This is our first test writing to a RTF file. '
  28.              'This first paragraph is in the preset style called normal '
  29.              'and any following paragraphs will use this style until we change it.')
  30.     section.append(p)
  31.    
  32.     p = Paragraph(ss.ParagraphStyles.Normal)
  33.     p.append('It is also possible to provide overrides for element of a style. ',
  34.             'For example you can change just the font',
  35.             TEXT(' size to 24 point', size=48),
  36.             ' or',
  37.             TEXT(' typeface to Impact', font=ss.Fonts.Impact),
  38.             ' or even more Attributes like',
  39.             TEXT(' BOLD',bold=True),
  40.             TEXT(' or Italic',italic=True),
  41.             TEXT(' or BOTH',bold=True,italic=True),
  42.             '.')
  43.     section.append(p)
  44.    
  45.     p = Paragraph()
  46.     p.append('This is a new paragraph with the word ',
  47.              TEXT('RED',colour=ss.Colours.Red),
  48.              ' in Red text.')
  49.     section.append(p)
  50.    
  51.     p = Paragraph(ss.ParagraphStyles.Courier)
  52.     p.append('Now we are using the Courier style at 8 points. '
  53.              'All subsequent paragraphs will use this style automatically. '
  54.              'This saves typing and is the default behaviour for RTF documents.',LINE)
  55.     section.append(p)
  56.     p = Paragraph()
  57.     p.append('Also notice that there is a blank line between the previous paragraph ',
  58.              'and this one.  That is because of the "LINE" inline command.')
  59.            
  60.     section.append(p)
  61.    
  62.     p = Paragraph(ss.ParagraphStyles.ArialBoldRed)
  63.     p.append(LINE,'And now we are using the ArialBoldRed style.',LINE)
  64.     section.append(p)
  65.        
  66.     return doc
  67.  
  68. def OpenFile(name) :
  69.     return file('%s.rtf' % name, 'w')
  70.  
  71. if __name__ == '__main__' :
  72.     DR = Renderer()
  73.     doc = MakeExample()
  74.     DR.Write(doc, OpenFile('rtfteste'))
  75.     print "Finished"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement