Advertisement
Guest User

Python n°24 - pyRTF

a guest
Aug 3rd, 2011
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from PyRTF import *
  4.  
  5. def FabriqueExemple():
  6.     docu = Document()
  7.     ss  = docu.StyleSheet
  8.     section = Section()
  9.     docu.Sections.append(section)
  10.  
  11.     result = docu.StyleSheet
  12.     NormalText = TextStyle(TextPropertySet(result.Fonts.CourierNew,16))
  13.     ps2 = ParagraphStyle('Courier',NormalText.Copy())
  14.     result.ParagraphStyles.append(ps2)
  15.  
  16.     # et le tout dernier pour l'Arial Gras Rouge en 11 points
  17.     TexteNormal = TextStyle(TextPropertySet(result.Fonts.Arial,22,bold=True,colour=ss.Colours.Red))
  18.     ps2 = ParagraphStyle('ArialGrasRouge',TexteNormal.Copy())
  19.     result.ParagraphStyles.append(ps2)
  20.  
  21.     p = Paragraph(ss.ParagraphStyles.Heading1)
  22.     p.append('Exemple d''en-tete')
  23.     section.append(p)
  24.  
  25.     p = Paragraph(ss.ParagraphStyles.Normal)
  26.     p.append('Voici notre premier exemple de creation de fichier RTF. '
  27.              'Ce premier paragraphe est dans le style predefini appele normal '
  28.              'et tous les paragraphes suivants utiliseront ce style sauf si on le change.')
  29.     section.append(p)
  30.  
  31.     p = Paragraph(ss.ParagraphStyles.Normal)
  32.     p.append( 'Il est aussi possible de passer outre les elements d''un style. ',
  33.             'Par exemple vous pouvez modifier seulement la ',
  34.             TEXT(' taille de la police a 24 points', size=48),
  35.             ' ou',
  36.             TEXT(' son type a Impact', font=ss.Fonts.Impact),
  37.             ' ou meme modifier d''autres attributs comme',
  38.             TEXT(' la graisse ',bold=True),
  39.             TEXT(' ou l''italique',italic=True),
  40.             TEXT(' ou les deux',bold=True,italic=True),
  41.             '.' )
  42.     section.append(p)
  43.  
  44.     p = Paragraph()
  45.     p.append('Voici un nouveau paragraphe avec le mot ',
  46.             TEXT('ROUGE',colour=ss.Colours.Red),
  47.             ' ecrit en rouge.')
  48.     section.append(p)
  49.  
  50.     p = Paragraph(ss.ParagraphStyles.Courier)
  51.     p.append('Maintenant on utilise le style Courier en 8 points. '
  52.              'Tous les paragraphes suivants utiliseront ce style automatiquement. '
  53.              'Ceci evite de resaisir du code et est le comportement par defaut pour les documents RTF.',LINE)
  54.     section.append(p)
  55.     p = Paragraph()
  56.     p.append('Remarquez aussi qu''il y a une ligne blanche entre le paragraphe precedent ',
  57.              'et celui-ci. C''est a cause de la commande en ligne LINE.')
  58.  
  59.     section.append(p)
  60.  
  61.     p = Paragraph(ss.ParagraphStyles.ArialGrasRouge)
  62.     p.append(LINE,'Et maintenant on utilise le style ArialGrasRouge.',LINE)
  63.     section.append(p)
  64.  
  65.     return docu
  66.  
  67. def OuvreFichier(nom) :
  68.     return file('%s.rtf' % nom, 'w')
  69.  
  70. if __name__ == '__main__' :
  71.     DR = Renderer()
  72.     docu = FabriqueExemple()
  73.     DR.Write(docu, OuvreFichier('rtftesta'))
  74.     print "Fini"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement