issue50:tutopython
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue50:tutopython [2011/06/26 22:58] – créée fredphil91 | issue50:tutopython [2011/08/07 08:31] (Version actuelle) – fredphil91 | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | 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. | + | **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. | 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. | ||
Ligne 7: | Ligne 7: | ||
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. | 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... | + | 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. | ||
- | import os | + | Cette fois-ci, nous allons traiter deux sujets. Le premier est l' |
+ | |||
+ | Impression générique sous Linux | ||
+ | |||
+ | Commençons donc avec l' | ||
+ | |||
+ | 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' | ||
+ | |||
+ | **import os | ||
pr = os.popen(' | pr = os.popen(' | ||
Ligne 18: | Ligne 27: | ||
pr.write(' | pr.write(' | ||
- | pr.close() | + | pr.close()** |
- | This is fairly easy to understand as long as you expand your mind just a bit. In the above code, ' | + | import os |
+ | pr = os.popen(' | ||
+ | pr.write('Test imprimante depuis linux via python\n') | ||
+ | pr.write('Impression terminee\n') | ||
+ | | ||
- | We can also create | + | **This is fairly easy to understand as long as you expand your mind just a bit. In the above code, ' |
- | import os | + | 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' | ||
+ | |||
+ | Nous pouvons également créer un fichier texte puis l' | ||
+ | |||
+ | **import os | ||
filename = ' | filename = ' | ||
Ligne 32: | Ligne 51: | ||
In this case, we are still using the lpr object, but we are using the ' | In this case, we are still using the lpr object, but we are using the ' | ||
- | I'll leave you to play with this for now. | + | I'll leave you to play with this for now.** |
- | PyRTF | + | import os |
+ | filename = ' | ||
+ | os.system(' | ||
+ | |||
+ | Dans ce cas, nous utilisons toujours l' | ||
+ | |||
+ | Je vous laisserai vous amuser un peu avec cela. | ||
+ | |||
+ | **PyRTF | ||
Now let's deal with RTF files. RTF format (that' | Now let's deal with RTF files. RTF format (that' | ||
- | 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' | ||
+ | |||
+ | 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. | + | **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 46: | Ligne 79: | ||
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. | 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. | + | 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.** |
- | Now for the meat of our worker routine MakeExample. Replace the pass statement with the code shown below. | + | 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. | ||
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. | 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. | ||
Ligne 54: | Ligne 95: | ||
That is very easy. Again, you need to plan your output fairly carefully, but nothing too onerous. | 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. | + | 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.** |
- | 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. | + | 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. | ||
p = Paragraph(ss.ParagraphStyles.Heading1) | p = Paragraph(ss.ParagraphStyles.Heading1) | ||
Ligne 66: | Ligne 115: | ||
Change the name of the rtf file to “rtftestb”. It should look like this: | Change the name of the rtf file to “rtftestb”. It should look like this: | ||
- | DR.Write(doc, | + | 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. | + | 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. | ||
Normal, Normal Short, Heading 1, Heading 2, Normal Numbered, Normal Numbered 2. There' | Normal, Normal Short, Heading 1, Heading 2, Normal Numbered, Normal Numbered 2. There' | ||
Ligne 74: | Ligne 133: | ||
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... | 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. | + | 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(' |
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. | 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. | ||
Ligne 82: | Ligne 149: | ||
Ok. If that all makes sense, what else can we do? | 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. | + | We can also set the color of the text within the TEXT inline command. Like this.** |
- | p = Paragraph() | + | 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.append(' | p.append(' | ||
Ligne 92: | Ligne 167: | ||
' | ' | ||
- | section.append(p) | + | section.append(p)** |
+ | |||
+ | p = Paragraph() | ||
+ | p.append(' | ||
+ | | ||
+ | ' | ||
+ | | ||
- | 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. |
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. | 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. | ||
Ligne 102: | Ligne 183: | ||
Arial, ArialBlack, ArialNarrow, | Arial, ArialBlack, ArialNarrow, | ||
- | 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 |
NormalText = TextStyle(TextPropertySet(result.Fonts.CourierNew, | NormalText = TextStyle(TextPropertySet(result.Fonts.CourierNew, | ||
Ligne 112: | Ligne 203: | ||
result.ParagraphStyles.append(ps2) | 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. | + | 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.** |
- | Now, before the return line at the bottom of the routine, let's include a new paragraph using the Courier style. | + | 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. | ||
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... | 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... | ||
Ligne 122: | Ligne 220: | ||
ps2 = ParagraphStyle(' | ps2 = ParagraphStyle(' | ||
- | result.ParagraphStyles.append(ps2) | + | result.ParagraphStyles.append(ps2)** |
- | And add the code below... | + | 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... | ||
p = Paragraph(ss.ParagraphStyles.ArialBoldRed) | p = Paragraph(ss.ParagraphStyles.ArialBoldRed) | ||
Ligne 132: | Ligne 238: | ||
section.append(p) | section.append(p) | ||
- | 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 |
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. | 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. | ||
Ligne 144: | Ligne 258: | ||
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. | 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... | + | 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...** |
- | doc = Document() | + | 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() | ||
ss = doc.StyleSheet | ss = doc.StyleSheet | ||
Ligne 160: | Ligne 286: | ||
| | ||
+ | | ||
+ | |||
+ | 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. |
c1 = Cell(Paragraph(' | c1 = Cell(Paragraph(' | ||
Ligne 170: | Ligne 307: | ||
c3 = Cell(Paragraph(' | c3 = Cell(Paragraph(' | ||
- | table.AddRow(c1, | + | table.AddRow(c1, |
- | Here we are setting the data that goes into each cell in the first row. | + | 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. | ||
c1 = Cell(Paragraph(ss.ParagraphStyles.Heading2,' | c1 = Cell(Paragraph(ss.ParagraphStyles.Heading2,' | ||
Ligne 182: | Ligne 326: | ||
table.AddRow(c1, | table.AddRow(c1, | ||
- | 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.** |
- | c1 = Cell(Paragraph(ss.ParagraphStyles.Heading2,' | + | 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,' | ||
c2 = Cell(Paragraph(ss.ParagraphStyles.Normal,' | c2 = Cell(Paragraph(ss.ParagraphStyles.Normal,' | ||
Ligne 198: | Ligne 351: | ||
return doc | return doc | ||
- | 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.** |
- | 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. | + | c1 = Cell(Paragraph(ss.ParagraphStyles.Heading2,'Style en-tete 2')) |
+ | c2 = Cell(Paragraph(ss.ParagraphStyles.Normal,'Retour au style Normal' | ||
+ | c3 = Cell(Paragraph(' | ||
+ | tableau.AddRow(c1, | ||
- | Save your file as rtftable-b.py. Now, delete everything between ' | + | Ceci règle la dernière ligne. |
- | thin_edge | + | 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 ' | ||
+ | |||
+ | Sauvegardez et exécutez l' | ||
+ | |||
+ | Sauvegardez votre fichier sous le nom rtfTableau-b.py, | ||
+ | |||
+ | **thin_edge | ||
thick_edge = BorderPS( width=80, style=BorderPS.SINGLE ) | thick_edge = BorderPS( width=80, style=BorderPS.SINGLE ) | ||
Ligne 224: | Ligne 393: | ||
c3 = Cell( Paragraph( ' | c3 = Cell( Paragraph( ' | ||
- | table.AddRow( c1, c2, c3 ) | + | table.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. | + | 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. | ||
c1 = Cell( Paragraph( ' | c1 = Cell( Paragraph( ' | ||
Ligne 244: | Ligne 427: | ||
c3 = Cell( Paragraph( ' | c3 = Cell( Paragraph( ' | ||
- | table.AddRow( c1, c2, c3 ) | + | table.AddRow( c1, c2, c3 )** |
- | Once again, cells in column 1 and three have a mixed frame in row three. | + | 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. | ||
section.append( table ) | section.append( table ) | ||
Ligne 254: | Ligne 451: | ||
See you next time! | See you next time! | ||
- | 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 319: | 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.1309121919.txt.gz · Dernière modification : 2011/06/26 22:58 de fredphil91