Outils pour utilisateurs

Outils du site


issue50:tutopython

WOW! It's hard to believe that this is the 24th issue already. Two years we've been learning Python! You've come a very long way. This time we are going to cover two topics. The first is printing to a printer, the second is creation of RTF (Rich Text Format) files for output. Generic Printing under Linux So let's start with printing to a printer. The idea to cover this came from an email sent by Gord Campbell. It's actually easy to do most printing from Linux, and easier than that other operating system that starts with “WIN” - and I won't deal with that OS. As long as all you want to print is straight text, no bold, italics, font changes, etc, it's fairly easy. Here's a simple app that will print directly to your printer…

Waouh ! Il est difficile de croire que ceci est déjà le 24ème numéro. Cela fait deux ans que nous apprenons le Python ! Vous avez parcouru un très long chemin.

Cette fois-ci, nous allons traiter deux sujets. Le premier est l'impression sur une imprimante, le second est la création de fichiers RTF (Rich Text Format, ou Format de Texte Riche) comme sortie.

Impression générique sous Linux

Commençons donc avec l'impression sur une imprimante. L'idée de parler de cela provenait d'un courriel envoyé par Gord Campbell. Il est réellement facile de faire la plupart des impressions depuis Linux ; plus facile qu'avec cet autre système d'exploitation qui commence par « WIN » - et dont je ne parlerai pas.

Tout est plutôt facile tant que vous ne souhaitez imprimer que du texte simple, sans gras, italique, changements de polices, etc. Voici une application simple qui permet d'imprimer directement sur votre imprimante …

import os pr = os.popen('lpr','w') pr.write('print test from linux via python\n') pr.write('Print finished\n') pr.close()

import os 
pr = os.popen('lpr','w') 
pr.write('Test imprimante depuis linux via python\n') 
pr.write('Impression terminee\n') 
pr.close()

This is fairly easy to understand as long as you expand your mind just a bit. In the above code, 'lpr' is the print spooler. The only requirement is that we have already configured 'lpd' and that it's running. More than likely, when you use a printer under Ubuntu, it's already done for you. 'Lpd' is usually referred to as a “magic-filter” that can automatically convert different types of documents to something the printer can understand. We are going to print to the 'lpr' device/object. Think of it simply as a file. We open the file. We have to import 'os'. Then in line 2, we open the 'lpr' with write access - assigning it to the object variable 'pr'. We then do a 'pr.write' with anything we want to print. Finally (line 5) we close the file, which will send the data out to the printer. We can also create a text file, then send it out to the printer like this…

C'est assez facile à comprendre si vous élargissez un peu votre esprit. Dans le code ci-dessus, « lpr » est le spooler d'impression. Le seul pré-requis est que nous ayons déjà configuré « lpd » et qu'il fonctionne. C'est très probablement déjà fait pour vous si vous utilisez une imprimante sous Ubuntu. « lpd » est généralement considéré comme un « filtre magique » qui permet de convertir automatiquement différents types de documents en quelque chose que l'imprimante peut comprendre. Nous allons imprimer sur le périphérique/objet « lpr ». Pensez-y comme à un simple fichier. Nous ouvrons le fichier ; nous devons importer « os ». Puis à la ligne 2, nous avons ouvert « lpr » avec un accès en écriture, en l'assignant à la variable objet « pr ». Nous procédons alors à une écriture « pr.write » avec tout ce que nous voulons imprimer. Enfin (ligne 5), nous fermons le fichier ce qui va envoyer les données vers l'imprimante.

Nous pouvons également créer un fichier texte puis l'envoyer à l'imprimante comme ceci…

import os filename = 'dummy.file' os.system('lpr %s' % filename) In this case, we are still using the lpr object, but we are using the 'os.system' command to basically create a command that looks to linux like we sent it from a terminal. I'll leave you to play with this for now.

import os
filename = 'fichier.bidon'
os.system('lpr %s' % filename)

Dans ce cas, nous utilisons toujours l'objet lpr mais avec la commande « os.system » qui sert simplement à envoyer à Linux une commande comme si on l'avait saisie depuis un terminal.

Je vous laisserai vous amuser un peu avec cela.

PyRTF Now let's deal with RTF files. RTF format (that's kind of like saying PIN number since PIN stands for Personal Identification Number, so that translates to Personal-Identification-Number Number. Something from the department of redundancy department, huh?) was originally created by the Microsoft Corporation in 1987, and its syntax was influenced by the TeX typesetting language. PyRTF is a wonderful library that makes it easy to write RTF files. You have to do some planning up front on how you want your files to look, but the results will be well worth it. First, you need to download and install the PyRTF package. Go to http://pyrtf.sourceforge.net and get the PyRTF-0.45.tar.gz package. Save it someplace and use archive manager to unpack it. Then using terminal, go to where you unpacked it. First we need to install the package, so type “sudo python setup.py install” and it will be installed for you. Notice there is an examples folder there. There's some good information there on how to do some advanced things.

pyRTF

Maintenant occupons-nous des fichiers RTF. Le format RTF (c'est comme quand on dit le numéro PIN puisque PIN signifie Numéro d'Identification Personnel et que ça revient à dire le Numéro Numéro d'Identification Personnel [Ndt : en français on n'a pas ce problème de redondance puisqu'on parle de code PIN] : ça dépend du département Département des Redondances, non ?) a été créé à l'origine par Microsoft en 1987 et sa syntaxe s'est inspirée du langage de composition de texte TeX. PyRTF est une merveilleuse bibliothèque qui facilite la création de fichiers RTF. Cela nécessite de réfléchir en amont à ce à quoi le fichier doit ressembler, mais le résultat en vaut vraiment la peine.

Tout d'abord il faut télécharger et installer le paquet pyRTF. Allez sur http://pyrtf.sourceforge.net et récupérez le paquet PyRTF-0.45.tar.gz. Sauvegardez-le quelque part et utilisez le gestionnaire d'archives pour le décompresser. Puis ouvrez un terminal et déplacez-vous à l'endroit où vous l'avez décompressé. Tout d'abord il faut installer le paquet, avec la commande « sudo python setup.py install ». Remarquez qu'il y a un répertoire d'exemples, qui contient de bonnes informations pour faire des choses un peu compliquées.

Here we go. Let's start as we usually do, creating the stub of our program which is shown on the next page, top right. Before going any further, we'll discuss what's going on. Line 2 imports the PyRTF library. Note that we are using a different import format than normal. This one imports everything from the library. Our main working routine is MakeExample. We've stubbed for now. The OpenFile routine creates the file for us with the name we pass into it, appends the extension “rtf”, puts it into the “write” mode, and returns a file handle. We've already discussed the if name routine before, but to refresh your memory, if we are running the program in a standalone mode, the internal variable name is set to “main”. If we call it as an import from another program, then it will just ignore that portion of the code. Here, we create an instance of the Renderer object, call the MakeExample routine, getting the returned object doc. We then write the file (in doc) using the OpenFile routine.

Nous y voilà. Commençons comme d'habitude en créant le canevas de notre programme que vous pouvez voir en haut à droite de la page suivante. Avant d'aller plus loin, parlons de ce qui se passe. La ligne 2 importe la bibliothèque pyRTF. Remarquez que nous utilisons un format d'importation différent des autres fois : cette fois-ci nous importons tout ce qui se trouve dans la biblothèque. Notre routine principale s'appelle FabriqueExemple et ne fait rien pour le moment. La routine OuvreFichier crée un fichier avec pour nom celui passé en argument, lui ajoute l'extension .rtf, le place en mode écriture et retourne un pointeur sur ce fichier.

Nous avons déjà parlé de la routine __name__ précédemment, mais pour vous rafraîchir la mémoire je vous rappelle que si nous exécutons le programme en mode autonome la variable interne __name__ est réglée à « __main__ » ; par contre, si on l'appelle comme « import » depuis un autre programme, cette portion de code sera ignorée.

Nous créons là une instance de l'objet Renderer, appelons la routine FabriqueExemple et récupérons l'objet retourné docu. Puis nous écrivons le fichier (docu) en utilisant la routine OuvreFichier.

Now for the meat of our worker routine MakeExample. Replace the pass statement with the code shown below. Let's look at what we have done. In the first line we create an instance of Document. Then we create an instance of the style sheet. Then we create an instance of the section object and append it to the document. Think of a section as a chapter in a book. Next we create a paragraph using the Normal style. The author of PyRTF has preset this to be 11-point Arial font. We then put whatever text we want into the paragraph, append that to the section, and return our doc document. That is very easy. Again, you need to plan your output fairly carefully, but nothing too onerous. Save the program as “rtftesta.py” and run it. When it's completed, use openoffice (or LibreOffice) to open the file and look at it.

Passons maintenant au contenu de la routine principale FabriqueExemple. Remplacez l'instruction pass par le code ci-dessous.

Regardons ce que nous avons fait. La première ligne crée une instance de document. Puis on crée une instance de feuille de style. Ensuite nous créons une instance de l'objet section et on l'ajoute au document. Imaginez une section comme un chapitre dans un livre. Ensuite nous créons un paragraphe en utilisant le style Normal. L'auteur de pyRTF a préréglé ce style avec une police Arial en 11 points. Ensuite on écrit le texte qu'on veut dans ce paragraphe, on l'ajoute à la section et on retourne notre document docu.

C'est vraiment facile. Encore une fois, vous devez réfléchir soigneusement en amont à la sortie désirée, mais ce n'est pas très compliqué.

Sauvegardez ce programme en tant que « rtftesta.py » et exécutez-le. Enfin, utilisez OpenOffice (ou LibreOffice) pour ouvrir le fichier et l'examiner.

Now let's do some neat things. First, we'll add a header. Once again, the author of PyRTF has given us a predefined style called Header1. We'll use that for our header. In between the doc.Sections.append line and the p = Paragraph line, add the following. p = Paragraph(ss.ParagraphStyles.Heading1) p.append('Example Heading 1') section.append(p) Change the name of the rtf file to “rtftestb”. It should look like this: DR.Write(doc, OpenFile('rtftestb'))

Maintenant faisons quelques modifications sympathiques. Tout d'abord, ajoutons un en-tête. Là encore l'auteur de pyRTF nous fournit un style prédéfini appelé Header1, que nous allons utiliser pour notre en-tête. Ajoutez ce qui suit entre les lignes docu.Sections.append et p = Paragraph.

p = Paragraph(ss.ParagraphStyles.Heading1)
p.append('Exemple d'en-tete')
section.append(p)

Modifiez le nom du fichier en « rtftestb » ; cela devrait donner ceci :

DR.Write(doc, OuvreFichier('rtftestb'))

Save this as rtftestb.py and run it. So now we have a header. I'm sure your mind is going down many roads thinking about what more can we do. Here's a list of what the author has given us as the predefined styles. Normal, Normal Short, Heading 1, Heading 2, Normal Numbered, Normal Numbered 2. There's also a List style, which I will let you play with on your own. If you want to see more, on this and other things, the styles are defined in the file Elements.py in the distribution you installed. While these styles are good for many things, we might want to use something other than the provided styles. Let's look at how to change fonts, font sizes and attributes (bold, italic, etc) on the fly. After our paragraph and before we return the document object, insert the code shown top right, and change the output filename to rtftestc. Save the file as rtftestc.py. And run it. The new portion of our document should look like this… It is also possible to provide overrides for elements of a style. For example you can change just the font size to 24 point or typeface to Impact or even more Attributes like BOLD or Italic or BOTH.

Sauvegardez-le sous le nom rtftestb.py et exécutez-le. Maintenant nous avons un en-tête. Je suis sûr que votre esprit est en train de se demander tout ce qu'on peut faire encore. Voici une liste des styles prédéfinis que l'auteur nous fournit.

Normal, Normal Short, Heading 1, Heading 2, Normal Numbered, Normal Numbered 2. Il y a également un style List que je vous laisserai découvrir. Si vous voulez en voir davantage, sur ça et sur d'autres sujets, les styles sont définis dans le fichier Elements.py que vous avez installé tout à l'heure.

Ces styles prédéfinis sont utiles pour beaucoup de choses, mais on peut avoir besoin d'en créer d'autres. Voyons maintenant comment modifier les polices, leur taille et leurs attributs (gras, italique, etc.) à la volée. Après notre paragraphe, et avant de retourner l'objet document, insérez le code ci-dessus à droite et modifiez le nom du fichier de sortie en rtftestc. Sauvegardez le fichier sous le nom rtftestc.py et exécutez-le. La nouvelle portion du document revrait ressembler à ceci…

Il est également possible de passer outre les éléments d'un style. Par exemple vous pouvez modifier seulement la taille de la police à 24 points, ou son type à Impact ou même modifier d'autres attributs comme la graisse ou l'italique ou les deux.

Now what have we done? Line 1 creates a new paragraph. We then start, as we did before, putting in our text. Look at the fourth line (TEXT(' size to 24 point', size = 48),). By using the TEXT qualifier, we are telling PyRTF to do something different in the middle of the sentence, which in this case is to change the size of the font (Arial at this point) to 24-point, followed by the 'size = ' command. But, wait a moment. The 'size =' says 48, and what we are printing says 24 point, and the output is actually in 24-point text. What's going on here? Well the size command is in half points. So if we want an 8-point font we have to use size = 16. Make sense? Next, we continue the text and then change the font with the 'font =' command. Again, everything within the inline TEXT command between the single quotes is going to be affected and nothing else. Ok. If that all makes sense, what else can we do? We can also set the color of the text within the TEXT inline command. Like this.

Bon, qu'avons-nous fait ? La ligne 1 crée un nouveau paragraphe. On commence comme auparavant à l'ajouter au texte. Regardez la ligne 4 (TEXT(' taille de la police a 24 points', size=48),) : en utilisant le qualificatif TEXT on indique à pyRTF qu'il faut faire quelque chose de différent au milieu de la phrase, dans ce cas on modifie la taille de la police (Arial) à 24 points, en précisant à la suite la commande « size = ». Mais attendez une minute : on indique 48 comme taille alors qu'on veut écrire en 24 points ; et la sortie est réellement en 24 points. Que se passe-t-il ici ? Eh bien, la commande de taille est en demi-points ; ainsi si on veut écrire en police 8 points, on doit utiliser « size = 16 ». Vous comprenez ?

Ensuite, on continue le texte et on modifie la police avec la commande « font = ». Cette fois encore, tout ce qui est dans l'instruction en ligne TEXT entre les guillemets sera affecté, mais pas le reste.

Bien. Si vous avez compris tout cela, que peut-on faire d'autre ?

On peut aussi régler la couleur du texte avec l'instruction en ligne TEXT de cette façon :

p = Paragraph() p.append('This is a new paragraph with the word ', TEXT('RED',colour=ss.Colours.Red), ' in Red text.') section.append(p)

p = Paragraph()
p.append('Voici un nouveau paragraphe avec le mot ',
   TEXT('ROUGE',colour=ss.Colours.Red),
   ' ecrit en rouge.')
section.append(p)

Notice that we didn't have to restate the paragraph style as Normal, since it sticks until we change it. Also notice that if you live in the U.S., you have to use the “proper” spelling of colour. Here are the colors that are (again) predefined: Black, Blue, Turquoise, Green, Pink, Red, Yellow, White, BlueDark, Teal, GreenDark, Violet, RedDark, YellowDark, GreyDark and Grey. And here is a list of all the predefined fonts (in the notation you must use to set them): Arial, ArialBlack, ArialNarrow, BitstreamVeraSans, BitstreamVeraSerif, BookAntiqua, BookmanOldStyle, BookmanOldStyle, Castellar, CenturyGothic, ComicSansMS, CourierNew, FranklinGothicMedium, Garamond, Georgia, Haettenschweiler, Impact, LucidaConsole, LucidaSansUnicode, MicrosoftSansSerif, PalatinoLinotype, MonotypeCorsiva, Papyrus, Sylfaen, Symbol, Tahoma, TimesNewRoman, TrebuchetMS and Verdana. So now you must be thinking that this is all well and good, but how do we make our own styles? That's pretty easy. Move to the top of our file, and before our header line, add the following code.

Remarquez que nous n'avons pas eu à repréciser que le style de paragraphe est Normal, puisqu'il ne change pas tant qu'on ne lui dit pas. Remarquez également que si vous habitez aux États-Unis vous devez utiliser la bonne orthographe pour « Colours » [Ndt : les américains utilisent souvent l'orthographe « impropre » Color].

Voici les couleurs prédéfinies : Black, Blue, Turquoise, Green, Pink, Red, Yellow, White, BlueDark, Teal, GreenDark, Violet, RedDark, YellowDark, GreyDark et Grey.

Et voici une liste de toutes les polices prédéfinies (ce sont les notations pour les utiliser) :

Arial, ArialBlack, ArialNarrow, BitstreamVeraSans, BitstreamVeraSerif, BookAntiqua, BookmanOldStyle, Castellar, CenturyGothic, ComicSansMS, CourierNew, FranklinGothicMedium, Garamond, Georgia, Haettenschweiler, Impact, LucidaConsole, LucidaSansUnicode, MicrosoftSansSerif, PalatinoLinotype, MonotypeCorsiva, Papyrus, Sylfaen, Symbol, Tahoma, TimesNewRoman, TrebuchetMS et Verdana.

Maintenant vous devez penser que tout cela est bien joli, mais comment peut-on créer ses propres styles ? C'est assez simple. Retournez en haut de notre fichier et ajoutez le code qui suit avant la ligne d'en-tête.

result = doc.StyleSheet NormalText = TextStyle(TextPropertySet(result.Fonts.CourierNew,16)) ps2 = ParagraphStyle('Courier',NormalText.Copy()) result.ParagraphStyles.append(ps2) Before we write the code to actually use it, let's see what we have done. We are creating a new stylesheet instance called result. In the second line, we are setting the font to 8-point Courier New, and then “registering” the style as Courier. Remember, we have to use 16 as the size since the font size is in half-point values.

result = docu.StyleSheet
NormalText = TextStyle(TextPropertySet(result.Fonts.CourierNew,16))
ps2 = ParagraphStyle('Courier',NormalText.Copy())
result.ParagraphStyles.append(ps2)

Avant d'écrire le code pour l'utiliser, regardons ce que nous avons fait. Nous créons une nouvelle instance feuille de style nommée result. À la deuxième ligne nous réglons la police à CourierNew en 8 points puis « enregistrons » le style comme Courier. Souvenez-vous que nous devons indiquer 16 comme taille puisque ce sont des demi-points.

Now, before the return line at the bottom of the routine, let's include a new paragraph using the Courier style. So now you have a new style you can use anytime you want. You can use any font in the list above and create your own styles. Simply copy the style code and replace the font and size information as you wish. We can also do this… NormalText = TextStyle(TextPropertySet(result.Fonts.Arial,22,bold=True,colour=ss.Colours.Red)) ps2 = ParagraphStyle('ArialBoldRed',NormalText.Copy()) result.ParagraphStyles.append(ps2)

Maintenant, ajoutons un nouveau paragraphe en utilisant le style Courier, avant la ligne return en bas de la routine.

Maintenant que vous avez un nouveau style, vous pouvez l'utiliser quand vous le souhaitez. Vous pouvez utiliser n'importe quelle police de la liste ci-dessus et créer vos propres styles. Recopiez simplement le code du style et remplacez les informations de police et de taille comme vous le voulez. On peut aussi faire cela :

TexteNormal = TextStyle(TextPropertySet(result.Fonts.Arial,22,bold=True,colour=ss.Colours.Red))
ps2 = ParagraphStyle('ArialGrasRouge',TexteNormal.Copy())
result.ParagraphStyles.append(ps2)

And add the code below… p = Paragraph(ss.ParagraphStyles.ArialBoldRed) p.append(LINE,'And now we are using the ArialBoldRed style.',LINE) section.append(p) to print the ArialBoldRed style.

Et ajouter le code suivant :

p = Paragraph(ss.ParagraphStyles.ArialGrasRouge)
p.append(LINE,'Et maintenant on utilise le style ArialGrasRouge.',LINE)
section.append(p)

pour afficher en style ArialGrasRouge.

Tables Many times, tables are the only way to properly represent data in a document. Doing tables in text is hard to do, and, in SOME cases, it's pretty easy in PyRTF. I'll explain this statement later in this article. Let's look at a standard table (shown below) in OpenOffice/LibreOffice. It looks like a spreadsheet, where everything ends up in columns. Rows go left to right, columns go down. Easy concept. Let's start a new application and call it rtfTable-a.py. Start with our standard code (shown on the next page) and build from there. We don't need to discuss this since it's basically the same code that we used before. Now, we'll flesh out the TableExample routine. I'm basically using part of the example file provided by the author of PyRTF. Replace the pass statement in the routine with the following code…

Tableaux

Souvent, la seule manière de présenter correctement des données dans un document est d'utiliser un tableau. Faire des tableaux dans un texte est plutôt difficile, mais PARFOIS c'est plutôt facile avec pyRTF. J'expliquerai cela plus tard dans cet article.

Regardons un tableau standard (ci-dessous) dans OpenOffice/LibreOffice. Cela ressemble à une feuille de calcul, où tout est placé dans des colonnes.

Des lignes horizontales, et des colonnes verticales. Un concept simple.

Commençons une nouvelle application nommée rtfTableau-a.py. Démarrons avec notre code standard (page suivante) et construisons à partir de là.

Pas besoin d'explications ici puisque c'est à peu près le même code que nous avons utilisé précédemment. Maintenant, écrivons la routine ExempleTableau. J'utilise en partie l'exemple fourni par l'auteur de pyRTF. Remplacez l'instruction pass dans la routine par le code suivant…

doc = Document() ss = doc.StyleSheet section = Section() doc.Sections.append(section) This part is the same as before, so we'll just gloss over it. table = Table(TabPS.DEFAULT_WIDTH * 7, TabPS.DEFAULT_WIDTH * 3, TabPS.DEFAULT_WIDTH * 3)

docu = Document()
ss = docu.StyleSheet
section = Section()
docu.Sections.append(section)

Cette partie est la même que précédemment, passons à la suite.

tableau = Table(TabPS.DEFAULT_WIDTH * 7,
   TabPS.DEFAULT_WIDTH * 3,
   TabPS.DEFAULT_WIDTH * 3)

This line (yes, it's really one line, but is broken up for easy viewing) creates our basic table. We are creating a table with 3 columns, the first is 7 tabs wide, the second and third are three tabs wide. We don't have to deal with tabs alone, you can enter the widths in twips. More on that in a moment. c1 = Cell(Paragraph('Row One, Cell One')) c2 = Cell(Paragraph('Row One, Cell Two')) c3 = Cell(Paragraph('Row One, Cell Three')) table.AddRow(c1,c2,c3)

Cette ligne (oui, il ne s'agit que d'une ligne, mais découpée pour plus de clarté) crée notre tableau basique. Nous créons un tableau à trois colonnes, la première contient 7 cellules, les deux suivantes en contiennent 3. Nous n'avons pas que des cellules uniques à notre disposition, car on pourra saisir les largeurs en « twips » [Ndt : ce sont des unités de mesure, utilisées en LibreOffice et pour le RTF, équivalentes à 1/1440 d'un pouce (2,54 cm). CF http://en.wikipedia.org/wiki/Twip#In_computing.] Nous y reviendrons dans un moment.

c1 = Cell(Paragraph('ligne 1, cellule 1'))
c2 = Cell(Paragraph('ligne 1, cellule 2'))
c3 = Cell(Paragraph('ligne 1, cellule 3'))
tableau.AddRow(c1,c2,c3)

Here we are setting the data that goes into each cell in the first row. c1 = Cell(Paragraph(ss.ParagraphStyles.Heading2,'Heading2 Style')) c2 = Cell(Paragraph(ss.ParagraphStyles.Normal,'Back to Normal Style')) c3 = Cell(Paragraph('More Normal Style')) table.AddRow(c1,c2,c3) This group of code sets the data for row number two. Notice we can set a different style for a single or multiple cells.

Ici nous réglons les données qui vont dans chaque cellule de la première ligne.

c1 = Cell(Paragraph(ss.ParagraphStyles.Heading2,'Style en-tete 2'))
c2 = Cell(Paragraph(ss.ParagraphStyles.Normal,'Retour au style Normal'))
c3 = Cell(Paragraph('Encore du style Normal'))
tableau.AddRow(c1,c2,c3)

Ce morceau de code règle les données pour la deuxième ligne. Remarquez que nous pouvons régler des styles différents pour une seule ou plusieurs cellules.

c1 = Cell(Paragraph(ss.ParagraphStyles.Heading2,'Heading2 Style')) c2 = Cell(Paragraph(ss.ParagraphStyles.Normal,'Back to Normal Style')) c3 = Cell(Paragraph('More Normal Style')) table.AddRow(c1,c2,c3) This sets the final row. section.append(table) return doc This appends the table into the section and returns the document for printing.

c1 = Cell(Paragraph(ss.ParagraphStyles.Heading2,'Style en-tete 2'))
c2 = Cell(Paragraph(ss.ParagraphStyles.Normal,'Retour au style Normal'))
c3 = Cell(Paragraph('Encore du style Normal'))
tableau.AddRow(c1,c2,c3)

Ceci règle la dernière ligne.

section.append(tableau)
return docu

Ceci ajoute le tableau dans la section et retourne le document pour affichage.

Save and run the app. You'll notice that everything is about what you would expect, but there is no border for the table. That can make things difficult. Let's fix that. Again, I'll mainly use code from the example file provided by the PyRTF author. Save your file as rtftable-b.py. Now, delete everything between 'doc.Sections.append(section)' and 'return doc' in the TableExample routine, and replace it with the following…

Sauvegardez et exécutez l'application. Vous remarquerez que tout ressemble à ce que vous attendiez, mais qu'il n'y a pas de bordures pour le tableau. Ceci peut rendre les choses difficiles à lire : réglons ce problème. À nouveau, j'utilise en grande partie le code de l'exemple fourni par l'auteur de pyRTF.

Sauvegardez votre fichier sous le nom rtfTableau-b.py, puis effacez tout ce qui est entre « docu.Sections.append(section) » et « return docu » dans la routine ExempleTableau, et remplacez-le par ce qui suit…

thin_edge = BorderPS( width=20, style=BorderPS.SINGLE ) thick_edge = BorderPS( width=80, style=BorderPS.SINGLE ) thin_frame = FramePS( thin_edge, thin_edge, thin_edge, thin_edge ) thick_frame = FramePS( thick_edge, thick_edge, thick_edge, thick_edge ) mixed_frame = FramePS( thin_edge, thick_edge, thin_edge, thick_edge ) Here we are setting up the edge and frame definitions for borders and frames. table = Table( TabPS.DEFAULT_WIDTH * 3, TabPS.DEFAULT_WIDTH * 3, TabPS.DEFAULT_WIDTH * 3 ) c1 = Cell( Paragraph( 'R1C1' ), thin_frame ) c2 = Cell( Paragraph( 'R1C2' ) ) c3 = Cell( Paragraph( 'R1C3' ), thick_frame ) table.AddRow( c1, c2, c3 )

cote_fin  = BorderPS( width=20, style=BorderPS.SINGLE )
cote_epais = BorderPS( width=80, style=BorderPS.SINGLE )
bord_fin  =  FramePS( cote_fin,   cote_fin,   cote_fin,   cote_fin )
bord_epais = FramePS( cote_epais, cote_epais, cote_epais, cote_epais )
bord_mixte = FramePS( cote_fin,   cote_epais, cote_fin,   cote_epais )

Ici nous réglons les définitions des côtés et des bords pour les encadrements.

tableau = Table( TabPS.DEFAULT_WIDTH * 3, TabPS.DEFAULT_WIDTH * 3, TabPS.DEFAULT_WIDTH * 3 )
c1 = Cell( Paragraph( 'L1C1' ), bord_fin )
c2 = Cell( Paragraph( 'L1C2' ) )
c3 = Cell( Paragraph( 'L1C3' ), bord_epais )
tableau.AddRow( c1, c2, c3 )

In row one, the cells in column one (thin frame) and column 3 (thick frame) will have a border around them. c1 = Cell( Paragraph( 'R2C1' ) ) c2 = Cell( Paragraph( 'R2C2' ) ) c3 = Cell( Paragraph( 'R2C3' ) ) table.AddRow( c1, c2, c3 ) None of the cells will have a border in the second row. c1 = Cell( Paragraph( 'R3C1' ), mixed_frame ) c2 = Cell( Paragraph( 'R3C2' ) ) c3 = Cell( Paragraph( 'R3C3' ), mixed_frame ) table.AddRow( c1, c2, c3 )

Dans la première ligne, les cellules de la colonne 1 (bord_fin) et de la colonne 3(bord_epais) auront une bordure.

c1 = Cell( Paragraph( 'L2C1' ) )
c2 = Cell( Paragraph( 'L2C2' ) )
c3 = Cell( Paragraph( 'L2C3' ) )
tableau.AddRow( c1, c2, c3 )

Aucune des cases n'aura de bordure dans la ligne 2.

c1 = Cell( Paragraph( 'L3C1' ), bord_mixte )
c2 = Cell( Paragraph( 'L3C2' ) )
c3 = Cell( Paragraph( 'L3C3' ), bord_mixte )
tableau.AddRow( c1, c2, c3 )

Once again, cells in column 1 and three have a mixed frame in row three. section.append( table ) So. You have just about everything you need to create, through code, RTF documents. See you next time! Source code can be found at pastebin as usual. The first part can be found at http://pastebin.com/3Rs7T3D7 which is the sum of rtftest.py (a-e), and the second rtftable.py (a-b) is at http://pastebin.com/XbaE2uP7.

À nouveau, les cases des colonnes 1 et 3 auront une bordure mixte dans la troisième ligne.

section.append( tableau )

Et voilà. Vous avez maintenant les bases pour créer des documents RTF avec du code.

À la prochaine fois !

Le code source est disponible sur pastebin comme d'habitude. La première partie est ici : http://pastebin.com/uRVrGjkV et contient le résumé de rtftest.py (a à e), la seconde partie rtfTableeau.py (a et b) est ici : http://pastebin.com/L8DGU7Lz.

CODE PAGE 9 (haut)

#!/usr/bin/env python
from PyRTF import *
def FabriqueExemple():
  pass
def OuvreFichier(nom) :
  return file('%s.rtf' % nom, 'w')
if __name__ == '__main__' :
  DR = Renderer()
  docu = FabriqueExemple()
  DR.Write(docu, OuvreFichier('rtftesta'))
  print "Fini"

CODE PAGE 9 (bas)

  docu = Document()
  ss  = docu.StyleSheet
  section = Section()
  docu.Sections.append(section)
  p = Paragraph(ss.ParagraphStyles.Normal)
  p.append('Voici notre premier exemple de création de fichier RTF. '
           'Ce premier paragraphe est dans le style prédéfini appelé normal '
           'et tous les paragraphes suivants utiliseront ce style sauf si on le change.')
  section.append(p)
  return docu

CODE PAGE 10

  p = Paragraph(ss.ParagraphStyles.Normal)
  p.append( 'Il est aussi possible de passer outre les elements d''un style. ',
          'Par exemple vous pouvez modifier seulement la ',
          TEXT(' taille de la police a 24 points', size=48),
          ' ou',
          TEXT(' son type a Impact', font=ss.Fonts.Impact),
          ' ou meme d''autres attributs comme',
          TEXT(' LA GRAISSE',bold=True),
          TEXT(' ou l''italique',italic=True),
          TEXT(' ou LES DEUX',bold=True,italic=True),
          '.' )
  section.append(p)

CODE PAGE 11

  p = Paragraph(ss.ParagraphStyles.Courier)
  p.append('Now we are using the Courier style at 8 points. '
           'All subsequent paragraphs will use this style automatically. '
           'This saves typing and is the default behaviour for RTF documents.',LINE)
  section.append(p)
  p = Paragraph()
  p.append('Also notice that there is a blank line between the previous paragraph ',
           'and this one.  That is because of the "LINE" inline command.')
          
  section.append(p)

CODE PAGE 12

#!/usr/bin/env python
from PyRTF import *
def ExempleTableau():
  pass
def OuvreFichier(nom):
  return file('%s.rtf' % nom, 'w')
if __name__ == '__main__':
  DR = Renderer()
  docu = ExempleTableau()
  DR.Write(docu, OuvreFichier('rtftable-a'))
  print "Fini"
issue50/tutopython.txt · Dernière modification : 2011/08/07 08:31 de fredphil91