issue50:tutopython
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue50:tutopython [2011/07/05 21:02] – fredphil91 | issue50:tutopython [2011/08/07 08:31] (Version actuelle) – fredphil91 | ||
---|---|---|---|
Ligne 11: | Ligne 11: | ||
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. | 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, nous allons | + | Cette fois-ci, nous allons |
Impression générique sous Linux | Impression générique sous Linux | ||
- | Commençons donc avec l' | + | Commençons donc avec l' |
- | Tout est plutôt | + | Tout est plutôt |
**import os | **import os | ||
Ligne 39: | Ligne 39: | ||
We can also create a text file, then send it out to the printer like this...** | 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, | + | C'est assez facile à comprendre si vous élargissez un peu votre esprit. Dans le code ci-dessus, |
Nous pouvons également créer un fichier texte puis l' | Nous pouvons également créer un fichier texte puis l' | ||
Ligne 53: | Ligne 53: | ||
I'll leave you to play with this for now.** | I'll leave you to play with this for now.** | ||
+ | import os | ||
+ | filename = ' | ||
+ | os.system(' | ||
+ | Dans ce cas, nous utilisons toujours l' | ||
+ | |||
+ | Je vous laisserai vous amuser un peu avec cela. | ||
**PyRTF | **PyRTF | ||
Ligne 61: | Ligne 67: | ||
First, you need to download and install the PyRTF package. Go to http:// | First, you need to download and install the PyRTF package. Go to http:// | ||
+ | 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' | ||
- | Here we go. Let's start as we usually do, creating the stub of our program which is shown on the next page, top r**ight. | + | Tout d' |
+ | |||
+ | **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. | 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. | 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. | ||
Ligne 71: | Ligne 81: | ||
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.** | 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' | ||
+ | Avant d' | ||
+ | Notre routine principale s' | ||
+ | Nous avons déjà parlé de la routine < | ||
+ | |||
+ | Nous créons là une instance de l' | ||
**Now for the meat of our worker routine MakeExample. Replace the pass statement with the code shown below. | **Now for the meat of our worker routine MakeExample. Replace the pass statement with the code shown below. | ||
Ligne 81: | Ligne 97: | ||
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.** | 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' | |
+ | |||
+ | 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' | ||
+ | |||
+ | 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' | ||
**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. | **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. | ||
Ligne 95: | Ligne 117: | ||
DR.Write(doc, | DR.Write(doc, | ||
+ | Maintenant faisons quelques modifications sympathiques. Tout d' | ||
+ | p = Paragraph(ss.ParagraphStyles.Heading1) | ||
+ | p.append(' | ||
+ | section.append(p) | ||
+ | |||
+ | Modifiez le nom du fichier en « rtftestb » ; cela devrait donner ceci : | ||
+ | |||
+ | DR.Write(doc, | ||
**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. | **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. | ||
Ligne 105: | Ligne 135: | ||
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.** | 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' | ||
+ | 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' | ||
+ | |||
+ | Ces styles prédéfinis sont utiles pour beaucoup de choses, mais on peut avoir besoin d'en créer d' | ||
+ | |||
+ | 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' | ||
**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(' | **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(' | ||
Ligne 115: | Ligne 151: | ||
We can also set the color of the text within the TEXT inline command. Like this.** | We can also set the color of the text within the TEXT inline command. Like this.** | ||
+ | Bon, qu' | ||
+ | |||
+ | Ensuite, on continue le texte et on modifie la police avec la commande « font = ». Cette fois encore, tout ce qui est dans l' | ||
+ | |||
+ | Bien. Si vous avez compris tout cela, que peut-on faire d' | ||
+ | On peut aussi régler la couleur du texte avec l' | ||
**p = Paragraph() | **p = Paragraph() | ||
Ligne 127: | Ligne 169: | ||
section.append(p)** | section.append(p)** | ||
+ | p = Paragraph() | ||
+ | p.append(' | ||
+ | | ||
+ | ' | ||
+ | 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. | **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. | ||
Ligne 139: | Ligne 185: | ||
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.** | 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' | ||
+ | |||
+ | 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, | ||
+ | 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' | ||
**result = doc.StyleSheet | **result = doc.StyleSheet | ||
Ligne 151: | Ligne 205: | ||
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.** | 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, | ||
+ | ps2 = ParagraphStyle(' | ||
+ | result.ParagraphStyles.append(ps2) | ||
+ | Avant d' | ||
**Now, before the return line at the bottom of the routine, let's include a new paragraph using the Courier style. | **Now, before the return line at the bottom of the routine, let's include a new paragraph using the Courier style. | ||
Ligne 163: | Ligne 222: | ||
result.ParagraphStyles.append(ps2)** | 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' | ||
+ | |||
+ | TexteNormal = TextStyle(TextPropertySet(result.Fonts.Arial, | ||
+ | ps2 = ParagraphStyle(' | ||
+ | result.ParagraphStyles.append(ps2) | ||
**And add the code below... | **And add the code below... | ||
Ligne 175: | Ligne 240: | ||
to print the ArialBoldRed style.** | to print the ArialBoldRed style.** | ||
+ | Et ajouter le code suivant : | ||
+ | |||
+ | p = Paragraph(ss.ParagraphStyles.ArialGrasRouge) | ||
+ | p.append(LINE,' | ||
+ | section.append(p) | ||
+ | pour afficher en style ArialGrasRouge. | ||
**Tables | **Tables | ||
Ligne 189: | Ligne 260: | ||
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...** | 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' | ||
+ | |||
+ | Regardons un tableau standard (ci-dessous) dans OpenOffice/ | ||
+ | |||
+ | Des lignes horizontales, | ||
+ | |||
+ | 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' | ||
**doc = Document() | **doc = Document() | ||
Ligne 207: | Ligne 288: | ||
| | ||
+ | docu = Document() | ||
+ | ss = docu.StyleSheet | ||
+ | section = Section() | ||
+ | docu.Sections.append(section) | ||
+ | Cette partie est la même que précédemment, | ||
+ | |||
+ | tableau = Table(TabPS.DEFAULT_WIDTH * 7, | ||
+ | | ||
+ | | ||
**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. | **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. | ||
Ligne 219: | Ligne 309: | ||
table.AddRow(c1, | table.AddRow(c1, | ||
+ | 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' | ||
+ | c1 = Cell(Paragraph(' | ||
+ | c2 = Cell(Paragraph(' | ||
+ | c3 = Cell(Paragraph(' | ||
+ | tableau.AddRow(c1, | ||
**Here we are setting the data that goes into each cell in the first row. | **Here we are setting the data that goes into each cell in the first row. | ||
Ligne 233: | Ligne 328: | ||
This group of code sets the data for row number two. Notice we can set a different style for a single or multiple cells.** | 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,' | ||
+ | c2 = Cell(Paragraph(ss.ParagraphStyles.Normal,' | ||
+ | c3 = Cell(Paragraph(' | ||
+ | tableau.AddRow(c1, | ||
+ | |||
+ | 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,' | **c1 = Cell(Paragraph(ss.ParagraphStyles.Heading2,' | ||
Ligne 251: | Ligne 353: | ||
This appends the table into the section and returns the document for printing.** | This appends the table into the section and returns the document for printing.** | ||
+ | c1 = Cell(Paragraph(ss.ParagraphStyles.Heading2,' | ||
+ | c2 = Cell(Paragraph(ss.ParagraphStyles.Normal,' | ||
+ | c3 = Cell(Paragraph(' | ||
+ | tableau.AddRow(c1, | ||
+ | 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 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. | ||
Ligne 257: | Ligne 369: | ||
Save your file as rtftable-b.py. Now, delete everything between ' | Save your file as rtftable-b.py. Now, delete everything between ' | ||
+ | Sauvegardez et exécutez l' | ||
+ | Sauvegardez votre fichier sous le nom rtfTableau-b.py, | ||
**thin_edge | **thin_edge | ||
Ligne 281: | Ligne 395: | ||
table.AddRow( c1, c2, c3 )** | table.AddRow( c1, c2, c3 )** | ||
+ | cote_fin | ||
+ | cote_epais = BorderPS( width=80, style=BorderPS.SINGLE ) | ||
+ | bord_fin | ||
+ | bord_epais = FramePS( cote_epais, cote_epais, cote_epais, cote_epais ) | ||
+ | bord_mixte = FramePS( cote_fin, | ||
+ | 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( ' | ||
+ | c2 = Cell( Paragraph( ' | ||
+ | c3 = Cell( Paragraph( ' | ||
+ | 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. | **In row one, the cells in column one (thin frame) and column 3 (thick frame) will have a border around them. | ||
Ligne 303: | Ligne 429: | ||
table.AddRow( c1, c2, c3 )** | 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( ' | ||
+ | c2 = Cell( Paragraph( ' | ||
+ | c3 = Cell( Paragraph( ' | ||
+ | tableau.AddRow( c1, c2, c3 ) | ||
+ | |||
+ | Aucune des cases n'aura de bordure dans la ligne 2. | ||
+ | |||
+ | c1 = Cell( Paragraph( ' | ||
+ | c2 = Cell( Paragraph( ' | ||
+ | c3 = Cell( Paragraph( ' | ||
+ | tableau.AddRow( c1, c2, c3 ) | ||
**Once again, cells in column 1 and three have a mixed frame in row three. | **Once again, cells in column 1 and three have a mixed frame in row three. | ||
Ligne 315: | Ligne 453: | ||
Source code can be found at pastebin as usual. The first part can be found at http:// | Source code can be found at pastebin as usual. The first part can be found at http:// | ||
+ | À 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' | ||
====== CODE PAGE 9 (haut) ====== | ====== CODE PAGE 9 (haut) ====== | ||
- | # | + | |
- | from PyRTF import * | + | from PyRTF import * |
- | def MakeExample(): | + | |
pass | pass | ||
- | def OpenFile(name) : | + | |
- | return file(' | + | return file(' |
- | if __name__ == ' | + | |
DR = Renderer() | DR = Renderer() | ||
- | | + | |
- | DR.Write(doc, OpenFile(' | + | DR.Write(docu, OuvreFichier(' |
- | print "Finished" | + | print "Fini" |
====== CODE PAGE 9 (bas) ====== | ====== CODE PAGE 9 (bas) ====== | ||
- | | + | |
- | ss = doc.StyleSheet | + | ss = docu.StyleSheet |
section = Section() | section = Section() | ||
- | | + | |
p = Paragraph(ss.ParagraphStyles.Normal) | p = Paragraph(ss.ParagraphStyles.Normal) | ||
- | p.append(' | + | p.append(' |
- | ' | + | ' |
- | ' | + | ' |
section.append(p) | section.append(p) | ||
- | return | + | return |
====== CODE PAGE 10 ====== | ====== CODE PAGE 10 ====== | ||
p = Paragraph(ss.ParagraphStyles.Normal) | p = Paragraph(ss.ParagraphStyles.Normal) | ||
- | p.append( 'It is also possible | + | p.append( 'Il est aussi possible |
- | 'For example you can change just the font ', | + | 'Par exemple vous pouvez modifier seulement la ', |
- | TEXT(' | + | TEXT(' |
- | ' | + | ' |
- | TEXT(' | + | TEXT(' |
- | ' | + | ' |
- | TEXT(' | + | TEXT(' |
- | TEXT(' | + | TEXT(' |
- | TEXT(' | + | TEXT(' |
' | ' | ||
section.append(p) | section.append(p) | ||
Ligne 379: | Ligne 525: | ||
====== CODE PAGE 12 ====== | ====== CODE PAGE 12 ====== | ||
- | # | + | |
- | from PyRTF import * | + | |
- | def TableExample(): | + | |
pass | pass | ||
- | def OpenFile(name): | + | |
- | return file(' | + | return file(' |
- | if __name__ == ' | + | |
DR = Renderer() | DR = Renderer() | ||
- | | + | |
- | DR.Write(doc, OpenFile(' | + | DR.Write(docu, OuvreFichier(' |
- | print "Finished" | + | print "Fini" |
issue50/tutopython.1309892528.txt.gz · Dernière modification : 2011/07/05 21:02 de fredphil91