-
Notifications
You must be signed in to change notification settings - Fork 19
/
demo.py
217 lines (196 loc) · 8.5 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# -*- coding: utf-8 -*-
__author__ = u'Tomasz Świderski <[email protected]>'
__copyright__ = u'Copyright (c) 2010 Tomasz Świderski'
import time
import random
from reportlab.lib.pagesizes import A4
from reportlab.platypus import BaseDocTemplate, Frame, PageTemplate
from reportlab.lib.units import mm
from reportlab.platypus.flowables import PageBreak, Spacer
from reportlab.platypus.paragraph import Paragraph
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from formula import CurrentPageColSum, PreviousPagesColSum, TotalPagesColSum, \
RowNumber
from spreadsheettable import SpreadsheetTable
styleSheet = getSampleStyleSheet()
MARGIN_SIZE = 25 * mm
PAGE_SIZE = A4
def get_test_data(cols, rows):
"""
Generates test data for Table objects.
"""
assert cols > 0
assert rows > 1
data = []
data.append(['Col %d' % col_num for col_num in xrange(cols)])
for row in xrange(rows - 1):
row_data = [row * cols + col for col in xrange(cols)]
data.append(row_data)
return data
def create_pdfdoc(pdfdoc, story):
"""
Creates PDF doc from story.
"""
pdf_doc = BaseDocTemplate(pdfdoc, pagesize = PAGE_SIZE,
leftMargin = MARGIN_SIZE, rightMargin = MARGIN_SIZE,
topMargin = MARGIN_SIZE, bottomMargin = MARGIN_SIZE)
main_frame = Frame(MARGIN_SIZE, MARGIN_SIZE,
PAGE_SIZE[0] - 2 * MARGIN_SIZE, PAGE_SIZE[1] - 2 * MARGIN_SIZE,
leftPadding = 0, rightPadding = 0, bottomPadding = 0,
topPadding = 0, id = 'main_frame')
main_template = PageTemplate(id = 'main_template', frames = [main_frame])
pdf_doc.addPageTemplates([main_template])
pdf_doc.build(story)
def demo():
"""
Runs demo demonstrating usage of Spreadsheet Tables.
"""
print 'Generating spreadsheet_demo.pdf in current working directiory...'
table_style = [
('GRID', (0,0), (-1,-1), 1, colors.black),
('ALIGN', (0,0), (-1,-1), 'CENTER'),
('LEFTPADDING', (0,0), (-1,-1), 3),
('RIGHTPADDING', (0,0), (-1,-1), 3),
('FONTSIZE', (0,0), (-1,-1), 10),
('FONTNAME', (0,0), (-1,0), 'Times-Bold'),
]
data = [
['Row No', 'Favourite movies'],
[RowNumber(), 'Star Wars'],
[RowNumber(), 'Back to the Future'],
[RowNumber(), 'Forrest Gump'],
[RowNumber(), 'Fight Club'],
[RowNumber(), 'The Matrix'],
[RowNumber(), 'Terminator'],
[RowNumber(), 'Alien'],
[RowNumber(), 'Die Hard'],
[RowNumber(), 'Blade Runner'],
[RowNumber(), 'Star Trek'],
[RowNumber(), 'V for Vendetta'],
[RowNumber(), 'Twelve Monkeys'],
[RowNumber(), 'The Truman Show'],
]
story = []
story.append(Paragraph("Spreadsheet Table usage demonstration",
styleSheet['Title']))
story.append(Paragraph("Row enumeration (the simplest possible formula \
example)", styleSheet['Heading2']))
story.append(Paragraph("This shows how to enumerate table rows starting \
from 1 on each page. With normal tables you can't do that, because it's \
impossible (or at least very hard) to predict where Table will split.", styleSheet['BodyText']))
story.append(Spacer(0, 10 * mm))
spreadsheet_table = SpreadsheetTable(data, repeatRows = 1)
spreadsheet_table.setStyle(table_style)
story.append(spreadsheet_table)
story.append(PageBreak())
story.append(Paragraph("This shows how enumeration behave with split.",
styleSheet['BodyText']))
story.append(Spacer(0, 10 * mm))
spreadsheet_table = SpreadsheetTable(data, repeatRows = 1)
spreadsheet_table.setStyle(table_style)
s = spreadsheet_table.split(PAGE_SIZE[0], 90)
for part in s:
story.append(part)
story.append(Spacer(0, 10 * mm))
story.append(PageBreak())
story.append(Paragraph("Repeating bottom rows", styleSheet['Heading2']))
story.append(Paragraph("This shows how to insert repeating rows into \
bottom of table. With normal Table you can only repeat top rows.",
styleSheet['BodyText']))
story.append(Spacer(0, 10 * mm))
data.append(['Row No', 'Favourite movies'])
table_style.append(['FONTNAME', (0,-1), (-1,-1), 'Times-Bold'])
spreadsheet_table = SpreadsheetTable(data, repeatRows = 1, repeatRowsB = 1)
spreadsheet_table.setStyle(table_style)
story.append(spreadsheet_table)
story.append(PageBreak())
story.append(Paragraph("This shows how repeating bottom rows behave with \
split.", styleSheet['BodyText']))
story.append(Spacer(0, 10 * mm))
spreadsheet_table = SpreadsheetTable(data, repeatRows = 1, repeatRowsB = 1)
spreadsheet_table.setStyle(table_style)
s = spreadsheet_table.split(PAGE_SIZE[0], 90)
for part in s:
story.append(part)
story.append(Spacer(0, 10 * mm))
story.append(PageBreak())
story.append(Paragraph("Summary rows", styleSheet['Heading2']))
story.append(Paragraph("You can insert formulas into repeating bottom \
rows to create summary rows. In this example I want to show you how to \
insert current page col sum, previous pages col sum and total pages col sum into summary row.", styleSheet['BodyText']))
story.append(Spacer(0, 10 * mm))
data = [
['Employee', 'Monthly salary'],
['Amy N. Jones', random.randint(2000, 6000)],
['Gloria T. Rodriguez', random.randint(2000, 6000)],
['Kyle S. Tyson', random.randint(2000, 6000)],
['Jeffrey C. Jackson', random.randint(2000, 6000)],
['Roger T. Hicks', random.randint(2000, 6000)],
['Maxine D. Luther', random.randint(2000, 6000)],
['Rita J. Colon', random.randint(2000, 6000)],
['Eva J. Bollinger', random.randint(2000, 6000)],
['William A. Fitch', random.randint(2000, 6000)],
['Martin E. Burke', random.randint(2000, 6000)],
['Richard A. Jones', random.randint(2000, 6000)],
['Richard A. Jone', random.randint(2000, 6000)],
['Stephen G. Pullen', random.randint(2000, 6000)],
['Previous pages salaries:', PreviousPagesColSum(decimal_places = 0)],
['Current page salaries:', CurrentPageColSum(decimal_places = 0)],
['Total salaries:', TotalPagesColSum(decimal_places = 0)],
]
table_style[-1] = ['FONTNAME', (0,-3), (-1,-1), 'Times-Bold']
spreadsheet_table = SpreadsheetTable(data, repeatRows = 1, repeatRowsB = 3)
spreadsheet_table.setStyle(table_style)
story.append(spreadsheet_table)
story.append(PageBreak())
story.append(Paragraph("This shows how summary rows behave with split.",
styleSheet['BodyText']))
story.append(Spacer(0, 10 * mm))
spreadsheet_table = SpreadsheetTable(data, repeatRows = 1, repeatRowsB = 3)
spreadsheet_table.setStyle(table_style)
s = spreadsheet_table.split(PAGE_SIZE[0], 160)
for part in s:
story.append(part)
story.append(Spacer(0, 10 * mm))
story.append(Paragraph("More...", styleSheet['Heading2']))
story.append(Paragraph("If you need other formulas to get your work done, \
please take a look into formula's source code. Formulas are very easy to \
write. If you write something useful, just let me know \
<[email protected]> and I will include it into repository.",
styleSheet['BodyText']))
create_pdfdoc('spreadsheet_demo.pdf', story)
def performance_test():
"""
Performance test for long tables.
"""
table_style = [
('GRID', (0,0), (-1,-1), 1, colors.black),
('ALIGN', (0,0), (-1,-1), 'CENTER'),
('LEFTPADDING', (0,0), (-1,-1), 3),
('RIGHTPADDING', (0,0), (-1,-1), 3),
('FONTSIZE', (0,0), (-1,-1), 10)
]
print 'LongTables performance test...'
for row_num in xrange(1000, 6000, 1000):
data = get_test_data(cols=10, rows=row_num)
a = time.time()
table = SpreadsheetTable(data, repeatRows = 1, style = table_style)
create_pdfdoc('spreadsheet_%d.pdf' % row_num, [table])
b = time.time()
msg = 'SpreadsheetTable generation time (%d rows): %s.'
msg %= (row_num, b - a)
print msg
for row_num in xrange(1000, 6000, 1000):
table_style.append(('SPAN', (0,0), (1,0)))
data = get_test_data(cols=10, rows=row_num)
data[0][1] = ''
a = time.time()
table = SpreadsheetTable(data, repeatRows = 1, style = table_style)
create_pdfdoc('spreadsheet_%d_span.pdf' % row_num, [table])
b = time.time()
msg = 'SpreadsheetTable generation time with span (%d rows): %s.'
msg %= (row_num, b - a)
print msg
if __name__ == '__main__':
demo()