forked from tne-ai/apps-maria-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaria_pdf.py
176 lines (135 loc) · 5.78 KB
/
maria_pdf.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
import json
from io import BytesIO
from reportlab.pdfgen import canvas
from reportlab.platypus import Paragraph, Image, SimpleDocTemplate, Spacer, Table, TableStyle, PageBreak
from reportlab.platypus import PageTemplate
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
import matplotlib.pyplot as plt
from tne.TNE import TNE
styles = getSampleStyleSheet()
spacer_blank = Spacer(1, 12)
spacer_enter = Spacer(1, 6)
#take in json file contents
#https://www.geeksforgeeks.org/json-load-in-python/
#https://docs.reportlab.com/reportlab/userguide/ch6_paragraphs/
# Initialize the TNE object
session = TNE(uid=UID, bucket_name=BUCKET, project=PROJECT, version=VERSION)
def pdf_maker(content, file_name):
#create pdf object, set bounds
pdf_buffer = BytesIO()
#contains pdf_buffer or file_name
pdf = SimpleDocTemplate(pdf_buffer, pagesize=letter)
styleN = styles['Normal']
#Create Cover Page
styleT = ParagraphStyle(
'Title',
fontSize=24,
fontName='Helvetica-Bold',
alignment=1,
spaceAfter=20,
leading=28
)
story = []
#TODO: delete when .model and .pdfformatter has been outfitted with optional cover page option
story.append(Spacer(1, 100))
story.append(Paragraph(file_name, styleT))
story.append(Spacer(1, 100))
story.append(PageBreak())
#iterate through each section
for section in content["sections"]:
content_type = section["type"]
actual_content = section["content"]
#TODO: Uncomment when .model and .pdfformatter has been outfitted with optional cover page option
# if content_type == "cover page":
# story.append(Spacer(1, 100))
# story.append(Paragraph(document_name, styleT))
# story.append(Spacer(1, 100))
# story.append(PageBreak())
if content_type == "raw text":
#have text/paragraph added to pdf (story object)
story.append(Paragraph(actual_content, styleN))
story.append(spacer_blank)
continue
elif content_type == "table":
#have table added into pdf
lines = actual_content.strip().split("\n")
headers = lines[0].split("|") # Extract headers
rows = [line.split("|") for line in lines[1:]] # Extract row
table_content = [headers] + rows
styleT = TableStyle([
('GRID', (0, 0), (-1, -1), 1, colors.black),
('BOX', (0, 0), (-1, -1), 1, colors.black),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 1), (-1, -1), 'Helvetica'),
('FONTNAME', (0, 1), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, -1), 10)
])
story.append(Table(table_content, style= styleT))
elif content_type == "chart":
#load in json formatted content into code
chart_contents = json.loads(actual_content)
#get the chart data
chart_datasets = chart_contents['data']
#get chart title
chart_title_display = chart_contents['options']['title']['display']
chart_title_text = chart_contents['options']['title']['text']
if(chart_title_display):
plt.title(chart_title_text)
#depending on type of chart listed
if(chart_contents['type'] == 'line'):
print("LINE CHART")
#get chart legend
chart_legend = chart_contents['options']['legend']['display']
#BUILD GRAPH HERE
# get x lables
x_axis = chart_datasets['labels']
# get dataset label
dataset_label = chart_datasets['datasets'][0]['label']
#get dataset
dataset = chart_datasets['datasets'][0]['data']
#build the graph
plt.plot(x_axis, dataset)
plt.ylabel(dataset_label)
if(chart_legend):
plt.legend()
#plt.show()
#Save chart to BytesIO buffer
chart_stream = BytesIO()
plt.savefig(chart_stream, format='png')
plt.close()
chart_stream.seek(0)
img = Image(chart_stream, width = 400, height = 300)
story.append(img)
elif(chart_contents['type'] == 'bar'):
#get x axis lables
x_axis = chart_datasets['labels']
#get dataset lables (y-axis)
dataset_label = chart_datasets['datasets'][0]['label']
#get the dataset
dataset = chart_datasets['datasets'][0]['data']
#build the bar chart
plt.bar(x_axis, dataset)
plt.xlabel('Years') #TODO: NEED TO FIX SO X-AXIS LABEL CAN BE SET
plt.ylabel(dataset_label)
#Save chart to BytesIO buffer
chart_stream = BytesIO()
plt.savefig(chart_stream, format='png')
plt.close()
chart_stream.seek(0)
img = Image(chart_stream, width = 400, height = 300)
story.append(img)
else:
print("SOMETHING ELSE")
continue
pdf.build(story)
#contains pdf_buffer or pdf
session.upload_object(file_name, pdf_buffer)
return file_name
#turn file into dictionary
json_contents = json.loads(PROCESS_INPUT)
file_name = json_contents["document_filename"]
content = json_contents
#call pdf_maker method
result = pdf_maker(content, file_name)