-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjinja_wp_report.py
187 lines (153 loc) · 5.02 KB
/
jinja_wp_report.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
from jinja2 import Template
from weasyprint import HTML
def write_html(html_path, html_line):
#==========================================================
#*** Add one line of HTML to the ongoing HTML string. ***
#==========================================================
global html_report
# Uncomment these to also create an HTML file identical to the PDF being generated.
#html_file = open(html_path, 'at+')
#html_file.write(html_line + '\n')
#html_file.close()
html_report += html_line
return #write_html
program_directory = '\\\\path\\to\\directory'
output_directory = program_directory + '\\output'
my_logo = '<img src="logo.png" alt="logo" height="123" width="185">'
program_html = output_directory + '\\sample_report.html'
program_pdf = output_directory + '\\sample_report.pdf'
head_open = '<!DOCTYPE html><html lang = "en"><head><title>Sample Report</title><style>'
head_close = '</style></head>'
body_open = '<body>'
body_close = '</body></html>'
css = """@page
{
size: Letter;
margin: 0.5cm;
}
body
{
color-adjust:exact;
font-family: Arial, Helvetica, sans-serif;
}
p.footer
{
font-weight: bold;
text-align: center;
}
table.header
{
border: 0px;
width: 100%;
}
table.data
{
border: 1px solid black;
border-collapse: collapse;
width: 100%;
}
td.data
{
border: 1px solid black;
padding-bottom:10px;
padding-left:10px;
padding-right:10px;
padding-top:10px;
text-align: right;
}
td.data_header
{
background: #94E1BA;
border: 1px solid black;
font-weight: bold;
padding-bottom:10px;
padding-left:0px;
padding-right:0px;
padding-top:10px;
text-align: center;
}
td.data_row_header
{
background: #94E1BA;
border: 1px solid black;
font-weight: bold;
padding-bottom:10px;
padding-left:10px;
padding-right:10px;
padding-top:10px;
text-align: left;
}
td.header
{
text-align: left;
vertical-align: top;
}"""
#========================================================================
#*** For HTML generation, .PNG location is relative to output file. ***
#*** For PDF generation, .PNG location is relative to this program. ***
#========================================================================
template_report_header = Template("""<table CLASS = "header">
<tr>
<td CLASS = "header" WIDTH = "10%"> </td>
<td CLASS = "header" WIDTH = "23%">{{ logo }}</td>
<td CLASS = "header" WIDTH = "10%"> </td>
<td CLASS = "header" WIDTH = "57%">
<p>{{ title_text }}</p>
</td>
</tr>
</table>""")
template_table_header = Template("""<table CLASS = "data">
<tr>
<td CLASS = "data_row_header">Sales Rep</td>
<td CLASS = "data_header">Q1</td>
<td CLASS = "data_header">Q2</td>
<td CLASS = "data_header">Q3</td>
<td CLASS = "data_header">Q4</td>
</tr>""")
template_table_row = Template("""<tr>
<td CLASS = "data_row_header">{{ rep_name }}</td>
<td CLASS = "data">{{ data_q1 }}</td>
<td CLASS = "data">{{ data_q2 }}</td>
<td CLASS = "data">{{ data_q3 }}</td>
<td CLASS = "data">{{ data_q4 }}</td>
</tr>""")
template_report_footer = Template("""</table>
<p CLASS = "footer">This data is complete as of {{ report_date }}.</p>""")
# Print one report.
html_report = ''
write_html(program_html, head_open)
write_html(program_html, css)
write_html(program_html, head_close)
write_html(program_html, body_open)
body = template_report_header.render(title_text = "This sample report was constructed using Jinja2 and WeasyPrint", logo = my_logo)
write_html(program_html, body)
body = template_table_header.render()
write_html(program_html, body)
body = template_table_row.render(rep_name = 'Alice Anderson',
data_q1 = 1303,
data_q2 = 567,
data_q3 = 2501,
data_q4 = 1200)
write_html(program_html, body)
body = template_table_row.render(rep_name = 'Bert Brentworth',
data_q1 = 455,
data_q2 = 1320,
data_q3 = 0,
data_q4 = 1473)
write_html(program_html, body)
body = template_table_row.render(rep_name = 'Carol Calico',
data_q1 = 890,
data_q2 = 962,
data_q3 = 833,
data_q4 = 1078)
write_html(program_html, body)
body = template_table_row.render(rep_name = 'Daniel Dyers',
data_q1 = 1004,
data_q2 = 2154,
data_q3 = 1687,
data_q4 = 934)
write_html(program_html, body)
body = template_report_footer.render(report_date = "01/01/2019")
write_html(program_html, body)
write_html(program_html, body_close)
HTML(string = html_report, base_url = '.').write_pdf(program_pdf)