author | robin |
Mon, 18 Nov 2013 17:51:20 +0000 | |
branch | py33 |
changeset 3790 | 2d62ae94f4cb |
parent 3617 | ae5744e97c42 |
child 3794 | 398ea04239b5 |
permissions | -rw-r--r-- |
3617 | 1 |
#Copyright ReportLab Europe Ltd. 2000-2012 |
2963 | 2 |
#see license.txt for license details |
3 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/docs/userguide/ch6_tables.py |
|
2966 | 4 |
from tools.docco.rl_doc_utils import * |
3483 | 5 |
from reportlab.platypus import Image,ListFlowable, ListItem |
2963 | 6 |
import reportlab |
7 |
||
8 |
heading1("Tables and TableStyles") |
|
9 |
disc(""" |
|
10 |
The $Table$ and $LongTable$ classes derive from the $Flowable$ class and are intended |
|
11 |
as a simple textual gridding mechanisms. The $longTable$ class uses a greedy algorithm |
|
12 |
when calculating column widths and is intended for long tables where speed counts. |
|
13 |
$Table$ cells can hold anything which can be converted to |
|
14 |
a <b>Python</b> $string$ or $Flowables$ (or lists of $Flowables$). |
|
15 |
""") |
|
16 |
||
17 |
disc(""" |
|
18 |
Our present tables are a trade-off between efficient drawing and specification |
|
19 |
and functionality. We assume the reader has some familiarity with HTML tables. |
|
20 |
In brief, they have the following characteristics: |
|
21 |
""") |
|
22 |
||
23 |
bullet("""They can contain anything convertible to a string; flowable |
|
24 |
objects such as other tables; or entire sub-stories""") |
|
25 |
||
26 |
bullet("""They can work out the row heights to fit the data if you don't supply |
|
27 |
the row height. (They can also work out the widths, but generally it is better |
|
28 |
for a designer to set the width manually, and it draws faster).""") |
|
29 |
||
30 |
bullet("""They can split across pages if needed (see the canSplit attribute). |
|
31 |
You can specify that a number of rows at the top and bottom should be |
|
32 |
repeated after the split (e.g. show the headers again on page 2,3,4...)""") |
|
33 |
||
34 |
bullet("""They have a simple and powerful notation for specifying shading and |
|
35 |
gridlines which works well with financial or database tables, where you |
|
36 |
don't know the number of rows up front. You can easily say 'make the last row |
|
37 |
bold and put a line above it'""") |
|
38 |
||
39 |
bullet("""The style and data are separated, so you can declare a handful of table |
|
40 |
styles and use them for a family of reports. Styes can also 'inherit', as with |
|
41 |
paragraphs.""") |
|
42 |
||
43 |
disc("""There is however one main limitation compared to an HTML table. |
|
44 |
They define a simple rectangular grid. There is no simple row or column |
|
45 |
spanning; if you need to span cells, you must nest tables inside table cells instead or use a more |
|
46 |
complex scheme in which the lead cell of a span contains the actual contents.""") |
|
47 |
||
48 |
disc(""" |
|
49 |
$Tables$ are created by passing the constructor an optional sequence of column widths, |
|
50 |
an optional sequence of row heights, and the data in row order. |
|
51 |
Drawing of the table can be controlled by using a $TableStyle$ instance. This allows control of the |
|
52 |
color and weight of the lines (if any), and the font, alignment and padding of the text. |
|
53 |
A primitive automatic row height and or column width calculation mechanism is provided for. |
|
54 |
""") |
|
55 |
||
56 |
heading2('$Table$ User Methods') |
|
57 |
disc("""These are the main methods which are of interest to the client programmer.""") |
|
58 |
||
59 |
heading4("""$Table(data, colWidths=None, rowHeights=None, style=None, splitByRow=1, |
|
60 |
repeatRows=0, repeatCols=0)$""") |
|
61 |
||
62 |
disc("""The $data$ argument is a sequence of sequences of cell values each of which |
|
63 |
should be convertible to a string value using the $str$ function or should be a Flowable instance (such as a $Paragraph$) or a list (or tuple) of such instances. |
|
64 |
If a cell value is a $Flowable$ or list of $Flowables$ these must either have a determined width |
|
65 |
or the containing column must have a fixed width. |
|
66 |
The first row of cell values |
|
67 |
is in $data[0]$ i.e. the values are in row order. The $i$, $j$<sup>th.</sup> cell value is in |
|
68 |
$data[i][j]$. Newline characters $'\\n'$ in cell values are treated as line split characters and |
|
69 |
are used at <i>draw</i> time to format the cell into lines. |
|
70 |
""") |
|
71 |
disc("""The other arguments are fairly obvious, the $colWidths$ argument is a sequence |
|
72 |
of numbers or possibly $None$, representing the widths of the columns. The number of elements |
|
73 |
in $colWidths$ determines the number of columns in the table. |
|
74 |
A value of $None$ means that the corresponding column width should be calculated automatically.""") |
|
75 |
||
76 |
disc("""The $rowHeights$ argument is a sequence |
|
77 |
of numbers or possibly $None$, representing the heights of the rows. The number of elements |
|
78 |
in $rowHeights$ determines the number of rows in the table. |
|
79 |
A value of $None$ means that the corresponding row height should be calculated automatically.""") |
|
80 |
||
81 |
disc("""The $style$ argument can be an initial style for the table.""") |
|
82 |
disc("""The $splitByRow$ argument is only needed for tables both too tall and too wide |
|
83 |
to fit in the current context. In this case you must decide whether to 'tile' |
|
84 |
down and across, or across and then down. This parameter is a Boolean indicating that the |
|
85 |
$Table$ should split itself |
|
86 |
by row before attempting to split itself by column when too little space is available in |
|
3044
7e5a05f30245
Fixed documentation: tables cannot be split by column.
jonas
parents:
2966
diff
changeset
|
87 |
the current drawing area and the caller wants the $Table$ to split. |
7e5a05f30245
Fixed documentation: tables cannot be split by column.
jonas
parents:
2966
diff
changeset
|
88 |
Splitting a $Table$ by column is currently not implemented, so setting $splitByRow$ to $False$ will result in a $NotImplementedError$.""") |
2963 | 89 |
|
3044
7e5a05f30245
Fixed documentation: tables cannot be split by column.
jonas
parents:
2966
diff
changeset
|
90 |
disc("""The $repeatRows$ argument specifies the number of leading rows |
7e5a05f30245
Fixed documentation: tables cannot be split by column.
jonas
parents:
2966
diff
changeset
|
91 |
that should be repeated when the $Table$ is asked to split itself. The $repeatCols$ argument is currently ignored as a $Table$ cannot be split by column.""") |
2963 | 92 |
heading4('$Table.setStyle(tblStyle)$') |
93 |
disc(""" |
|
94 |
This method applies a particular instance of class $TableStyle$ (discussed below) |
|
95 |
to the $Table$ instance. This is the only way to get $tables$ to appear |
|
96 |
in a nicely formatted way. |
|
97 |
""") |
|
98 |
disc(""" |
|
99 |
Successive uses of the $setStyle$ method apply the styles in an additive fashion. |
|
100 |
That is, later applications override earlier ones where they overlap. |
|
101 |
""") |
|
102 |
||
103 |
heading2('$TableStyle$') |
|
104 |
disc(""" |
|
105 |
This class is created by passing it a sequence of <i>commands</i>, each command |
|
106 |
is a tuple identified by its first element which is a string; the remaining |
|
107 |
elements of the command tuple represent the start and stop cell coordinates |
|
108 |
of the command and possibly thickness and colors, etc. |
|
109 |
""") |
|
110 |
heading2("$TableStyle$ User Methods") |
|
111 |
heading3("$TableStyle(commandSequence)$") |
|
112 |
disc("""The creation method initializes the $TableStyle$ with the argument |
|
113 |
command sequence as an example:""") |
|
114 |
eg(""" |
|
115 |
LIST_STYLE = TableStyle( |
|
116 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
|
117 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
118 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
119 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
120 |
) |
|
121 |
""") |
|
122 |
heading3("$TableStyle.add(commandSequence)$") |
|
123 |
disc("""This method allows you to add commands to an existing |
|
124 |
$TableStyle$, i.e. you can build up $TableStyles$ in multiple statements. |
|
125 |
""") |
|
126 |
eg(""" |
|
127 |
LIST_STYLE.add('BACKGROUND', (0,0), (-1,0), colors.Color(0,0.7,0.7)) |
|
128 |
""") |
|
129 |
heading3("$TableStyle.getCommands()$") |
|
130 |
disc("""This method returns the sequence of commands of the instance.""") |
|
131 |
eg(""" |
|
132 |
cmds = LIST_STYLE.getCommands() |
|
133 |
""") |
|
134 |
heading2("$TableStyle$ Commands") |
|
135 |
disc("""The commands passed to $TableStyles$ come in three main groups |
|
136 |
which affect the table background, draw lines, or set cell styles. |
|
137 |
""") |
|
138 |
disc("""The first element of each command is its identifier, |
|
139 |
the second and third arguments determine the cell coordinates of |
|
140 |
the box of cells which are affected with negative coordinates |
|
141 |
counting backwards from the limit values as in <b>Python</b> |
|
142 |
indexing. The coordinates are given as |
|
143 |
(column, row) which follows the spreadsheet 'A1' model, but not |
|
144 |
the more natural (for mathematicians) 'RC' ordering. |
|
145 |
The top left cell is (0, 0) the bottom right is (-1, -1). Depending on |
|
146 |
the command various extra (???) occur at indices beginning at 3 on. |
|
147 |
""") |
|
148 |
heading3("""$TableStyle$ Cell Formatting Commands""") |
|
149 |
disc("""The cell formatting commands all begin with an identifier, followed by |
|
150 |
the start and stop cell definitions and the perhaps other arguments. |
|
151 |
the cell formatting commands are:""") |
|
152 |
eg(""" |
|
153 |
FONT - takes fontname, optional fontsize and optional leading. |
|
154 |
FONTNAME (or FACE) - takes fontname. |
|
155 |
FONTSIZE (or SIZE) - takes fontsize in points; leading may get out of sync. |
|
156 |
LEADING - takes leading in points. |
|
157 |
TEXTCOLOR - takes a color name or (R,G,B) tuple. |
|
158 |
ALIGNMENT (or ALIGN) - takes one of LEFT, RIGHT and CENTRE (or CENTER) or DECIMAL. |
|
159 |
LEFTPADDING - takes an integer, defaults to 6. |
|
160 |
RIGHTPADDING - takes an integer, defaults to 6. |
|
161 |
BOTTOMPADDING - takes an integer, defaults to 3. |
|
162 |
TOPPADDING - takes an integer, defaults to 3. |
|
163 |
BACKGROUND - takes a color. |
|
164 |
ROWBACKGROUNDS - takes a list of colors to be used cyclically. |
|
165 |
COLBACKGROUNDS - takes a list of colors to be used cyclically. |
|
166 |
VALIGN - takes one of TOP, MIDDLE or the default BOTTOM |
|
167 |
""") |
|
168 |
disc("""This sets the background cell color in the relevant cells. |
|
169 |
The following example shows the $BACKGROUND$, and $TEXTCOLOR$ commands in action:""") |
|
170 |
EmbeddedCode(""" |
|
171 |
data= [['00', '01', '02', '03', '04'], |
|
172 |
['10', '11', '12', '13', '14'], |
|
173 |
['20', '21', '22', '23', '24'], |
|
174 |
['30', '31', '32', '33', '34']] |
|
175 |
t=Table(data) |
|
176 |
t.setStyle(TableStyle([('BACKGROUND',(1,1),(-2,-2),colors.green), |
|
177 |
('TEXTCOLOR',(0,0),(1,-1),colors.red)])) |
|
178 |
""") |
|
179 |
disc("""To see the effects of the alignment styles we need some widths |
|
180 |
and a grid, but it should be easy to see where the styles come from.""") |
|
181 |
EmbeddedCode(""" |
|
182 |
data= [['00', '01', '02', '03', '04'], |
|
183 |
['10', '11', '12', '13', '14'], |
|
184 |
['20', '21', '22', '23', '24'], |
|
185 |
['30', '31', '32', '33', '34']] |
|
186 |
t=Table(data,5*[0.4*inch], 4*[0.4*inch]) |
|
187 |
t.setStyle(TableStyle([('ALIGN',(1,1),(-2,-2),'RIGHT'), |
|
188 |
('TEXTCOLOR',(1,1),(-2,-2),colors.red), |
|
189 |
('VALIGN',(0,0),(0,-1),'TOP'), |
|
190 |
('TEXTCOLOR',(0,0),(0,-1),colors.blue), |
|
191 |
('ALIGN',(0,-1),(-1,-1),'CENTER'), |
|
192 |
('VALIGN',(0,-1),(-1,-1),'MIDDLE'), |
|
193 |
('TEXTCOLOR',(0,-1),(-1,-1),colors.green), |
|
194 |
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
|
195 |
('BOX', (0,0), (-1,-1), 0.25, colors.black), |
|
196 |
])) |
|
197 |
""") |
|
198 |
heading3("""$TableStyle$ Line Commands""") |
|
199 |
disc(""" |
|
200 |
Line commands begin with the identifier, the start and stop cell coordinates |
|
201 |
and always follow this with the thickness (in points) and color of the desired lines. Colors can be names, |
|
202 |
or they can be specified as a (R, G, B) tuple, where R, G and B are floats and (0, 0, 0) is black. The line |
|
203 |
command names are: GRID, BOX, OUTLINE, INNERGRID, LINEBELOW, LINEABOVE, LINEBEFORE |
|
204 |
and LINEAFTER. BOX and OUTLINE are equivalent, and GRID is the equivalent of applying both BOX and |
|
205 |
INNERGRID. |
|
206 |
""") |
|
207 |
CPage(4.0) |
|
208 |
disc("""We can see some line commands in action with the following example. |
|
209 |
""") |
|
210 |
EmbeddedCode(""" |
|
211 |
data= [['00', '01', '02', '03', '04'], |
|
212 |
['10', '11', '12', '13', '14'], |
|
213 |
['20', '21', '22', '23', '24'], |
|
214 |
['30', '31', '32', '33', '34']] |
|
215 |
t=Table(data,style=[('GRID',(1,1),(-2,-2),1,colors.green), |
|
216 |
('BOX',(0,0),(1,-1),2,colors.red), |
|
217 |
('LINEABOVE',(1,2),(-2,2),1,colors.blue), |
|
218 |
('LINEBEFORE',(2,1),(2,-2),1,colors.pink), |
|
219 |
]) |
|
220 |
""") |
|
221 |
disc("""Line commands cause problems for tables when they split; the following example |
|
222 |
shows a table being split in various positions""") |
|
223 |
EmbeddedCode(""" |
|
224 |
data= [['00', '01', '02', '03', '04'], |
|
225 |
['10', '11', '12', '13', '14'], |
|
226 |
['20', '21', '22', '23', '24'], |
|
227 |
['30', '31', '32', '33', '34']] |
|
228 |
t=Table(data,style=[ |
|
229 |
('GRID',(0,0),(-1,-1),0.5,colors.grey), |
|
230 |
('GRID',(1,1),(-2,-2),1,colors.green), |
|
231 |
('BOX',(0,0),(1,-1),2,colors.red), |
|
232 |
('BOX',(0,0),(-1,-1),2,colors.black), |
|
233 |
('LINEABOVE',(1,2),(-2,2),1,colors.blue), |
|
234 |
('LINEBEFORE',(2,1),(2,-2),1,colors.pink), |
|
235 |
('BACKGROUND', (0, 0), (0, 1), colors.pink), |
|
236 |
('BACKGROUND', (1, 1), (1, 2), colors.lavender), |
|
237 |
('BACKGROUND', (2, 2), (2, 3), colors.orange), |
|
238 |
]) |
|
239 |
""") |
|
240 |
t=getStory()[-1] |
|
241 |
getStory().append(Spacer(0,6)) |
|
242 |
for s in t.split(4*inch,30): |
|
243 |
getStory().append(s) |
|
244 |
getStory().append(Spacer(0,6)) |
|
245 |
getStory().append(Spacer(0,6)) |
|
246 |
for s in t.split(4*inch,36): |
|
247 |
getStory().append(s) |
|
248 |
getStory().append(Spacer(0,6)) |
|
249 |
||
250 |
disc("""When unsplit and split at the first or second row.""") |
|
251 |
||
252 |
CPage(4.0) |
|
253 |
heading3("""Complex Cell Values""") |
|
254 |
disc(""" |
|
255 |
As mentioned above we can have complicated cell values including $Paragraphs$, $Images$ and other $Flowables$ |
|
256 |
or lists of the same. To see this in operation consider the following code and the table it produces. |
|
257 |
Note that the $Image$ has a white background which will obscure any background you choose for the cell. |
|
258 |
To get better results you should use a transparent background. |
|
259 |
""") |
|
260 |
import os, reportlab.platypus |
|
261 |
I = '../images/replogo.gif' |
|
262 |
EmbeddedCode(""" |
|
263 |
I = Image('%s') |
|
264 |
I.drawHeight = 1.25*inch*I.drawHeight / I.drawWidth |
|
265 |
I.drawWidth = 1.25*inch |
|
266 |
P0 = Paragraph(''' |
|
267 |
<b>A pa<font color=red>r</font>a<i>graph</i></b> |
|
268 |
<super><font color=yellow>1</font></super>''', |
|
269 |
styleSheet["BodyText"]) |
|
270 |
P = Paragraph(''' |
|
271 |
<para align=center spaceb=3>The <b>ReportLab Left |
|
272 |
<font color=red>Logo</font></b> |
|
273 |
Image</para>''', |
|
274 |
styleSheet["BodyText"]) |
|
275 |
data= [['A', 'B', 'C', P0, 'D'], |
|
276 |
['00', '01', '02', [I,P], '04'], |
|
277 |
['10', '11', '12', [P,I], '14'], |
|
278 |
['20', '21', '22', '23', '24'], |
|
279 |
['30', '31', '32', '33', '34']] |
|
280 |
||
281 |
t=Table(data,style=[('GRID',(1,1),(-2,-2),1,colors.green), |
|
282 |
('BOX',(0,0),(1,-1),2,colors.red), |
|
283 |
('LINEABOVE',(1,2),(-2,2),1,colors.blue), |
|
284 |
('LINEBEFORE',(2,1),(2,-2),1,colors.pink), |
|
285 |
('BACKGROUND', (0, 0), (0, 1), colors.pink), |
|
286 |
('BACKGROUND', (1, 1), (1, 2), colors.lavender), |
|
287 |
('BACKGROUND', (2, 2), (2, 3), colors.orange), |
|
288 |
('BOX',(0,0),(-1,-1),2,colors.black), |
|
289 |
('GRID',(0,0),(-1,-1),0.5,colors.black), |
|
290 |
('VALIGN',(3,0),(3,0),'BOTTOM'), |
|
291 |
('BACKGROUND',(3,0),(3,0),colors.limegreen), |
|
292 |
('BACKGROUND',(3,1),(3,1),colors.khaki), |
|
293 |
('ALIGN',(3,1),(3,1),'CENTER'), |
|
294 |
('BACKGROUND',(3,2),(3,2),colors.beige), |
|
295 |
('ALIGN',(3,2),(3,2),'LEFT'), |
|
296 |
]) |
|
297 |
||
298 |
t._argW[3]=1.5*inch |
|
299 |
"""%I) |
|
300 |
heading3("""$TableStyle$ Span Commands""") |
|
301 |
disc("""Our $Table$ classes support the concept of spanning, but it isn't specified in the same way |
|
302 |
as html. The style specification |
|
303 |
""") |
|
304 |
eg(""" |
|
305 |
SPAN, (sc,sr), (ec,er) |
|
306 |
""") |
|
307 |
disc("""indicates that the cells in columns $sc$ - $ec$ and rows $sr$ - $er$ should be combined into a super cell |
|
308 |
with contents determined by the cell $(sc, sr)$. The other cells should be present, but should contain empty strings |
|
309 |
or you may get unexpected results. |
|
310 |
""") |
|
311 |
EmbeddedCode(""" |
|
312 |
data= [['Top\\nLeft', '', '02', '03', '04'], |
|
313 |
['', '', '12', '13', '14'], |
|
314 |
['20', '21', '22', 'Bottom\\nRight', ''], |
|
315 |
['30', '31', '32', '', '']] |
|
316 |
t=Table(data,style=[ |
|
317 |
('GRID',(0,0),(-1,-1),0.5,colors.grey), |
|
318 |
('BACKGROUND',(0,0),(1,1),colors.palegreen), |
|
319 |
('SPAN',(0,0),(1,1)), |
|
320 |
('BACKGROUND',(-2,-2),(-1,-1), colors.pink), |
|
321 |
('SPAN',(-2,-2),(-1,-1)), |
|
322 |
]) |
|
323 |
""") |
|
324 |
||
325 |
disc("""notice that we don't need to be conservative with our $GRID$ command. The spanned cells are not drawn through. |
|
326 |
""") |
|
327 |
heading3("""Special $TableStyle$ Indeces""") |
|
328 |
disc("""In any style command the first row index may be set to one of the special strings |
|
329 |
$'splitlast'$ or $'splitfirst'$ to indicate that the style should be used only for the last row of |
|
330 |
a split table, or the first row of a continuation. This allows splitting tables with nicer effects around the split.""") |
|
331 |
||
3090 | 332 |
heading1("""Programming $Flowables$""") |
333 |
||
334 |
disc("""The following flowables let you conditionally evaluate and execute expressions and statements at wrap time:""") |
|
335 |
||
336 |
heading2("""$DocAssign(self, var, expr, life='forever')$""") |
|
337 |
||
338 |
disc("""Assigns a variable of name $var$ to the expression $expr$. E.g.:""") |
|
339 |
||
340 |
eg(""" |
|
341 |
DocAssign('i',3) |
|
342 |
""") |
|
343 |
||
344 |
heading2("""$DocExec(self, stmt, lifetime='forever')$""") |
|
345 |
||
346 |
disc("""Executes the statement $stmt$. E.g.:""") |
|
347 |
||
348 |
eg(""" |
|
349 |
DocExec('i-=1') |
|
350 |
""") |
|
351 |
||
352 |
heading2("""$DocPara(self, expr, format=None, style=None, klass=None, escape=True)$""") |
|
353 |
||
354 |
disc("""Creates a paragraph with the value of expr as text. |
|
355 |
If format is specified it should use %(__expr__)s for string interpolation |
|
356 |
of the expression expr (if any). It may also use %(name)s interpolations |
|
357 |
for other variables in the namespace. E.g.:""") |
|
358 |
||
359 |
eg(""" |
|
360 |
DocPara('i',format='The value of i is %(__expr__)d',style=normal) |
|
361 |
""") |
|
362 |
||
363 |
heading2("""$DocAssert(self, cond, format=None)$""") |
|
364 |
||
365 |
disc("""Raises an $AssertionError$ containing the $format$ string if $cond$ evaluates as $False$.""") |
|
366 |
||
367 |
eg(""" |
|
368 |
DocAssert(val, 'val is False') |
|
369 |
""") |
|
370 |
||
371 |
heading2("""$DocIf(self, cond, thenBlock, elseBlock=[])$""") |
|
372 |
||
373 |
disc("""If $cond$ evaluates as $True$, this flowable is replaced by the $thenBlock$ elsethe $elseBlock$.""") |
|
374 |
||
375 |
eg(""" |
|
376 |
DocIf('i>3',Paragraph('The value of i is larger than 3',normal),\\ |
|
377 |
Paragraph('The value of i is not larger than 3',normal)) |
|
378 |
""") |
|
379 |
||
380 |
heading2("""$DocWhile(self, cond, whileBlock)$""") |
|
381 |
||
382 |
disc("""Runs the $whileBlock$ while $cond$ evaluates to $True$. E.g.:""") |
|
383 |
||
384 |
eg(""" |
|
385 |
DocAssign('i',5) |
|
386 |
DocWhile('i',[DocPara('i',format='The value of i is %(__expr__)d',style=normal),DocExec('i-=1')]) |
|
387 |
""") |
|
388 |
||
389 |
disc("""This example produces a set of paragraphs of the form:""") |
|
390 |
||
391 |
eg(""" |
|
392 |
The value of i is 5 |
|
393 |
The value of i is 4 |
|
394 |
The value of i is 3 |
|
395 |
The value of i is 2 |
|
396 |
The value of i is 1 |
|
397 |
""") |
|
398 |
||
2963 | 399 |
heading1("""Other Useful $Flowables$""") |
3603
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
400 |
heading2("""$Preformatted(text, style, bulletText=None, dedent=0, maxLineLength=None, splitChars=None, newLineChars=None)$""") |
2963 | 401 |
disc(""" |
402 |
Creates a preformatted paragraph which does no wrapping, line splitting or other manipulations. |
|
403 |
No $XML$ style tags are taken account of in the text. |
|
404 |
If dedent is non zero $dedent$ common leading |
|
405 |
spaces will be removed from the front of each line. |
|
406 |
""") |
|
3603
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
407 |
heading3("Defining a maximum line length") |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
408 |
disc(""" |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
409 |
You can use the property $maxLineLength$ to define a maximum line length. If a line length exceeds this maximum value, the line will be automatically splitted. |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
410 |
""") |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
411 |
disc(""" |
3615 | 412 |
The line will be split on any single character defined in $splitChars$. If no value is provided for this property, the line will be split on any of the following standard characters: space, colon, full stop, semi-colon, coma, hyphen, forward slash, back slash, left parenthesis, left square bracket and left curly brace |
3603
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
413 |
""") |
2963 | 414 |
disc(""" |
3603
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
415 |
Characters can be automatically inserted at the beginning of each line that has been created. You can set the property $newLineChars$ to the characters you want to use. |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
416 |
""") |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
417 |
EmbeddedCode(""" |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
418 |
from reportlab.lib.styles import getSampleStyleSheet |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
419 |
stylesheet=getSampleStyleSheet() |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
420 |
normalStyle = stylesheet['Code'] |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
421 |
text=''' |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
422 |
class XPreformatted(Paragraph): |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
423 |
def __init__(self, text, style, bulletText = None, frags=None, caseSensitive=1): |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
424 |
self.caseSensitive = caseSensitive |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
425 |
if maximumLineLength and text: |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
426 |
text = self.stopLine(text, maximumLineLength, splitCharacters) |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
427 |
cleaner = lambda text, dedent=dedent: string.join(_dedenter(text or '',dedent),'') |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
428 |
self._setup(text, style, bulletText, frags, cleaner) |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
429 |
''' |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
430 |
t=Preformatted(text,normalStyle,maxLineLength=60, newLineChars='> ') |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
431 |
""") |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
432 |
heading2("""$XPreformatted(text, style, bulletText=None, dedent=0, frags=None)$""") |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
433 |
disc(""" |
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
434 |
This is a non rearranging form of the $Paragraph$ class; XML tags are allowed in |
2963 | 435 |
$text$ and have the same meanings as for the $Paragraph$ class. |
436 |
As for $Preformatted$, if dedent is non zero $dedent$ common leading |
|
437 |
spaces will be removed from the front of each line. |
|
438 |
""") |
|
439 |
EmbeddedCode(""" |
|
440 |
from reportlab.lib.styles import getSampleStyleSheet |
|
441 |
stylesheet=getSampleStyleSheet() |
|
3603
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
442 |
normalStyle = stylesheet['Code'] |
2963 | 443 |
text=''' |
444 |
||
445 |
This is a non rearranging form of the <b>Paragraph</b> class; |
|
446 |
<b><font color=red>XML</font></b> tags are allowed in <i>text</i> and have the same |
|
447 |
||
448 |
meanings as for the <b>Paragraph</b> class. |
|
3790 | 449 |
As for <b>Preformatted</b>, if dedent is non zero <font color="red" size="+1">dedent</font> |
2963 | 450 |
common leading spaces will be removed from the |
451 |
front of each line. |
|
452 |
You can have &amp; style entities as well for & < > and ". |
|
453 |
||
454 |
''' |
|
455 |
t=XPreformatted(text,normalStyle,dedent=3) |
|
456 |
""") |
|
3603
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
457 |
|
2963 | 458 |
heading2("""$Image(filename, width=None, height=None)$""") |
459 |
disc("""Create a flowable which will contain the image defined by the data in file $filename$. |
|
460 |
The default <b>PDF</b> image type <i>jpeg</i> is supported and if the <b>PIL</b> extension to <b>Python</b> |
|
461 |
is installed the other image types can also be handled. If $width$ and or $height$ are specified |
|
462 |
then they determine the dimension of the displayed image in <i>points</i>. If either dimension is |
|
463 |
not specified (or specified as $None$) then the corresponding pixel dimension of the image is assumed |
|
464 |
to be in <i>points</i> and used. |
|
465 |
""") |
|
3603
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
466 |
I="../images/lj8100.jpg" |
2963 | 467 |
eg(""" |
468 |
Image("lj8100.jpg") |
|
469 |
""",after=0.1) |
|
470 |
disc("""will display as""") |
|
471 |
try: |
|
472 |
getStory().append(Image(I)) |
|
473 |
except: |
|
474 |
disc("""An image should have appeared here.""") |
|
475 |
disc("""whereas""") |
|
476 |
eg(""" |
|
477 |
im = Image("lj8100.jpg", width=2*inch, height=2*inch) |
|
478 |
im.hAlign = 'CENTER' |
|
479 |
""", after=0.1) |
|
480 |
disc('produces') |
|
481 |
try: |
|
482 |
im = Image(I, width=2*inch, height=2*inch) |
|
483 |
im.hAlign = 'CENTER' |
|
484 |
getStory().append(Image(I, width=2*inch, height=2*inch)) |
|
485 |
except: |
|
486 |
disc("""An image should have appeared here.""") |
|
487 |
heading2("""$Spacer(width, height)$""") |
|
488 |
disc("""This does exactly as would be expected; it adds a certain amount of space into the story. |
|
489 |
At present this only works for vertical space. |
|
490 |
""") |
|
491 |
CPage(1) |
|
492 |
heading2("""$PageBreak()$""") |
|
493 |
disc("""This $Flowable$ represents a page break. It works by effectively consuming all vertical |
|
494 |
space given to it. This is sufficient for a single $Frame$ document, but would only be a |
|
495 |
frame break for multiple frames so the $BaseDocTemplate$ mechanism |
|
496 |
detects $pageBreaks$ internally and handles them specially. |
|
497 |
""") |
|
498 |
CPage(1) |
|
499 |
heading2("""$CondPageBreak(height)$""") |
|
500 |
disc("""This $Flowable$ attempts to force a $Frame$ break if insufficient vertical space remains |
|
501 |
in the current $Frame$. It is thus probably wrongly named and should probably be renamed as |
|
502 |
$CondFrameBreak$. |
|
503 |
""") |
|
504 |
CPage(1) |
|
505 |
heading2("""$KeepTogether(flowables)$""") |
|
506 |
disc(""" |
|
507 |
This compound $Flowable$ takes a list of $Flowables$ and attempts to keep them in the same $Frame$. |
|
508 |
If the total height of the $Flowables$ in the list $flowables$ exceeds the current frame's available |
|
509 |
space then all the space is used and a frame break is forced. |
|
510 |
""") |
|
3059
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
511 |
CPage(1) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
512 |
heading2("""$TableOfContents()$""") |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
513 |
disc(""" |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
514 |
A table of contents can be generated by using the $TableOfContents$ flowable. |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
515 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
516 |
The following steps are needed to add a table of contents to your document: |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
517 |
""") |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
518 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
519 |
disc("""Create an instance of $TableOfContents$. Override the level styles (optional) and add the object to the story:""") |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
520 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
521 |
eg(""" |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
522 |
toc = TableOfContents() |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
523 |
PS = ParagraphStyle |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
524 |
toc.levelStyles = [ |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
525 |
PS(fontName='Times-Bold', fontSize=14, name='TOCHeading1', |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
526 |
leftIndent=20, firstLineIndent=-20, spaceBefore=5, leading=16), |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
527 |
PS(fontSize=12, name='TOCHeading2', |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
528 |
leftIndent=40, firstLineIndent=-20, spaceBefore=0, leading=12), |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
529 |
PS(fontSize=10, name='TOCHeading3', |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
530 |
leftIndent=60, firstLineIndent=-20, spaceBefore=0, leading=12), |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
531 |
PS(fontSize=10, name='TOCHeading4', |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
532 |
leftIndent=100, firstLineIndent=-20, spaceBefore=0, leading=12), |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
533 |
] |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
534 |
story.append(toc) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
535 |
""") |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
536 |
|
3061
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
537 |
disc("""Entries to the table of contents can be done either manually by calling |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
538 |
the $addEntry$ method on the $TableOfContents$ object or automatically by sending |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
539 |
a $'TOCEntry'$ notification in the $afterFlowable$ method of the $DocTemplate$ |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
540 |
you are using. |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
541 |
|
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
542 |
The data to be passed to $notify$ is a list of three or four items countaining |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
543 |
a level number, the entry text, the page number and an optional destination key |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
544 |
which the entry should point to. |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
545 |
This list will usually be created in a document template's method |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
546 |
like afterFlowable(), making notification calls using the notify() |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
547 |
method with appropriate data like this: |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
548 |
""") |
3059
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
549 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
550 |
eg(''' |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
551 |
def afterFlowable(self, flowable): |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
552 |
"""Detect Level 1 and 2 headings, build outline, |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
553 |
and track chapter title.""" |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
554 |
if isinstance(flowable, Paragraph): |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
555 |
txt = flowable.getPlainText() |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
556 |
if style == 'Heading1': |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
557 |
# ... |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
558 |
self.notify('TOCEntry', (0, txt, self.page)) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
559 |
elif style == 'Heading2': |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
560 |
# ... |
3061
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
561 |
key = 'h2-%s' % self.seq.nextf('heading2') |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
562 |
self.canv.bookmarkPage(key) |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
563 |
self.notify('TOCEntry', (1, txt, self.page, key)) |
3059
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
564 |
# ... |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
565 |
''') |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
566 |
|
3061
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
567 |
disc("""This way, whenever a paragraph of style $'Heading1'$ or $'Heading2'$ is added to the story, it will appear in the table of contents. |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
568 |
$Heading2$ entries will be clickable because a bookmarked key has been supplied. |
ecd4963cc832
Userguide documentation for clickable TableOfContents entries.
jonas
parents:
3059
diff
changeset
|
569 |
""") |
3059
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
570 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
571 |
disc("""Finally you need to use the $multiBuild$ method of the DocTemplate because tables of contents need several passes to be generated:""") |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
572 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
573 |
eg(""" |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
574 |
doc.multiBuild(story) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
575 |
""") |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
576 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
577 |
disc("""Below is a simple but working example of a document with a table of contents:""") |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
578 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
579 |
eg(''' |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
580 |
from reportlab.lib.styles import ParagraphStyle as PS |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
581 |
from reportlab.platypus import PageBreak |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
582 |
from reportlab.platypus.paragraph import Paragraph |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
583 |
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
584 |
from reportlab.platypus.tableofcontents import TableOfContents |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
585 |
from reportlab.platypus.frames import Frame |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
586 |
from reportlab.lib.units import cm |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
587 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
588 |
class MyDocTemplate(BaseDocTemplate): |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
589 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
590 |
def __init__(self, filename, **kw): |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
591 |
self.allowSplitting = 0 |
3326 | 592 |
BaseDocTemplate.__init__(self, filename, **kw) |
3059
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
593 |
template = PageTemplate('normal', [Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')]) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
594 |
self.addPageTemplates(template) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
595 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
596 |
def afterFlowable(self, flowable): |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
597 |
"Registers TOC entries." |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
598 |
if flowable.__class__.__name__ == 'Paragraph': |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
599 |
text = flowable.getPlainText() |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
600 |
style = flowable.style.name |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
601 |
if style == 'Heading1': |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
602 |
self.notify('TOCEntry', (0, text, self.page)) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
603 |
if style == 'Heading2': |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
604 |
self.notify('TOCEntry', (1, text, self.page)) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
605 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
606 |
h1 = PS(name = 'Heading1', |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
607 |
fontSize = 14, |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
608 |
leading = 16) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
609 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
610 |
h2 = PS(name = 'Heading2', |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
611 |
fontSize = 12, |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
612 |
leading = 14, |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
613 |
leftIndent = delta) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
614 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
615 |
# Build story. |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
616 |
story = [] |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
617 |
toc = TableOfContents() |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
618 |
# For conciseness we use the same styles for headings and TOC entries |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
619 |
toc.levelStyles = [h1, h2] |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
620 |
story.append(toc) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
621 |
story.append(PageBreak()) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
622 |
story.append(Paragraph('First heading', h1)) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
623 |
story.append(Paragraph('Text in first heading', PS('body'))) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
624 |
story.append(Paragraph('First sub heading', h2)) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
625 |
story.append(Paragraph('Text in first sub heading', PS('body'))) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
626 |
story.append(PageBreak()) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
627 |
story.append(Paragraph('Second sub heading', h2)) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
628 |
story.append(Paragraph('Text in second sub heading', PS('body'))) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
629 |
story.append(Paragraph('Last heading', h1)) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
630 |
|
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
631 |
doc = MyDocTemplate('mintoc.pdf') |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
632 |
doc.multiBuild(story) |
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
3044
diff
changeset
|
633 |
''') |
3173 | 634 |
|
635 |
CPage(1) |
|
636 |
heading2("""$SimpleIndex()$""") |
|
637 |
disc(""" |
|
638 |
An index can be generated by using the $SimpleIndex$ flowable. |
|
639 |
||
640 |
The following steps are needed to add an index to your document: |
|
641 |
""") |
|
642 |
||
643 |
disc("""Use the index tag in paragraphs to index terms:""") |
|
644 |
||
645 |
eg(''' |
|
646 |
story = [] |
|
647 |
||
648 |
... |
|
649 |
||
650 |
story.append('The third <index item="word" />word of this paragraph is indexed.') |
|
651 |
''') |
|
652 |
||
653 |
disc("""Create an instance of $SimpleIndex$ and add it to the story where you want it to appear:""") |
|
654 |
||
655 |
eg(''' |
|
656 |
index = SimpleIndex(dot=' . ', headers=headers) |
|
657 |
story.append(index) |
|
658 |
''') |
|
659 |
||
660 |
disc("""The parameters which you can pass into the SimpleIndex constructor are explained in the reportlab reference. Now, build the document by using the canvas maker returned by SimpleIndex.getCanvasMaker():""") |
|
661 |
||
662 |
eg(""" |
|
663 |
doc.build(story, canvasmaker=index.getCanvasMaker()) |
|
664 |
""") |
|
665 |
||
666 |
disc("""To build an index with multiple levels, pass a comma-separated list of items to the item attribute of an index tag:""") |
|
667 |
||
668 |
eg(""" |
|
669 |
<index item="terma,termb,termc" /> |
|
670 |
<index item="terma,termd" /> |
|
671 |
""") |
|
672 |
||
673 |
disc("""terma will respresent the top-most level and termc the most specific term. termd and termb will appear in the same level inside terma.""") |
|
674 |
||
675 |
disc("""If you need to index a term containing a comma, you will need to escape it by doubling it. To avoid the ambiguity of three consecutive commas (an escaped comma followed by a list separator or a list separator followed by an escaped comma?) introduce a space in the right position. Spaces at the beginning or end of terms will be removed.""") |
|
676 |
||
677 |
eg(""" |
|
678 |
<index item="comma(,,), ,, ,... " /> |
|
679 |
""") |
|
680 |
||
681 |
disc(""" |
|
682 |
This indexes the terms "comma (,)", "," and "...". |
|
3326 | 683 |
""") |
3483 | 684 |
|
685 |
heading2("""$ListFlowable(),ListItem()$""") |
|
686 |
disc(""" |
|
687 |
Use these classes to make ordered and unordered lists. Lists can be nested. |
|
688 |
""") |
|
689 |
||
690 |
disc(""" |
|
691 |
$ListFlowable()$ will create an ordered list, which can contain any flowable. The class has a number of parameters to change font, colour, size, style and position of list numbers, or of bullets in unordered lists. The type of numbering can also be set to use lower or upper case letters ('A,B,C' etc.) or Roman numerals (capitals or lowercase) using the bulletType property. To change the list to an unordered type, set bulletType='bullet'. |
|
692 |
""") |
|
693 |
||
694 |
disc(""" |
|
695 |
Items within a $ListFlowable()$ list can be changed from their default appearance by wrapping them in a $ListItem()$ class and setting its properties. |
|
696 |
""") |
|
697 |
||
698 |
disc(""" |
|
699 |
The following will create an ordered list, and set the third item to an unordered sublist. |
|
700 |
""") |
|
701 |
||
702 |
EmbeddedCode(""" |
|
703 |
from reportlab.platypus import ListFlowable, ListItem |
|
704 |
from reportlab.lib.styles import getSampleStyleSheet |
|
705 |
styles = getSampleStyleSheet() |
|
706 |
style = styles["Normal"] |
|
707 |
t = ListFlowable( |
|
708 |
[ |
|
709 |
Paragraph("Item no.1", style), |
|
710 |
ListItem(Paragraph("Item no. 2", style),bulletColor="green",value=7), |
|
711 |
ListFlowable( |
|
712 |
[ |
|
713 |
Paragraph("sublist item 1", style), |
|
714 |
ListItem(Paragraph('sublist item 2', style),bulletColor='red',value='square') |
|
715 |
], |
|
716 |
bulletType='bullet', |
|
717 |
start='square', |
|
718 |
), |
|
719 |
Paragraph("Item no.4", style), |
|
720 |
], |
|
721 |
bulletType='i' |
|
722 |
) |
|
3603
dd1d19c9cb98
Added documentation for preformatted text wrapping and property tables for charts
guillaume
parents:
3485
diff
changeset
|
723 |
""") |