1
|
|
|
import base64 |
2
|
|
|
import uuid |
3
|
|
|
import os |
4
|
|
|
from openpyxl.chart import ( |
5
|
|
|
PieChart, |
6
|
|
|
BarChart, |
7
|
|
|
Reference, |
8
|
|
|
) |
9
|
|
|
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
10
|
|
|
from openpyxl.drawing.image import Image |
11
|
|
|
from openpyxl import Workbook |
12
|
|
|
from openpyxl.chart.label import DataLabelList |
13
|
|
|
|
14
|
|
|
#################################################################################################################### |
15
|
|
|
# PROCEDURES |
16
|
|
|
# Step 1: Validate the report data |
17
|
|
|
# Step 2: Generate excel file |
18
|
|
|
# Step 3: Encode the excel file bytes to Base64 |
19
|
|
|
#################################################################################################################### |
20
|
|
|
|
21
|
|
|
|
22
|
|
View Code Duplication |
def export(report, |
|
|
|
|
23
|
|
|
name, |
24
|
|
|
reporting_start_datetime_local, |
25
|
|
|
reporting_end_datetime_local, |
26
|
|
|
period_type): |
27
|
|
|
#################################################################################################################### |
28
|
|
|
# Step 1: Validate the report data |
29
|
|
|
#################################################################################################################### |
30
|
|
|
if report is None: |
31
|
|
|
return None |
32
|
|
|
print(report) |
33
|
|
|
|
34
|
|
|
#################################################################################################################### |
35
|
|
|
# Step 2: Generate excel file from the report data |
36
|
|
|
#################################################################################################################### |
37
|
|
|
filename = generate_excel(report, |
38
|
|
|
name, |
39
|
|
|
reporting_start_datetime_local, |
40
|
|
|
reporting_end_datetime_local, |
41
|
|
|
period_type) |
42
|
|
|
#################################################################################################################### |
43
|
|
|
# Step 3: Encode the excel file to Base64 |
44
|
|
|
#################################################################################################################### |
45
|
|
|
try: |
46
|
|
|
with open(filename, 'rb') as binary_file: |
47
|
|
|
binary_file_data = binary_file.read() |
48
|
|
|
except IOError as ex: |
49
|
|
|
pass |
50
|
|
|
|
51
|
|
|
# Base64 encode the bytes |
52
|
|
|
base64_encoded_data = base64.b64encode(binary_file_data) |
|
|
|
|
53
|
|
|
# get the Base64 encoded data using human-readable characters. |
54
|
|
|
base64_message = base64_encoded_data.decode('utf-8') |
55
|
|
|
# delete the file from server |
56
|
|
|
try: |
57
|
|
|
os.remove(filename) |
58
|
|
|
except NotImplementedError as ex: |
59
|
|
|
pass |
60
|
|
|
return base64_message |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def generate_excel(report, |
64
|
|
|
name, |
65
|
|
|
reporting_start_datetime_local, |
66
|
|
|
reporting_end_datetime_local, |
67
|
|
|
period_type): |
68
|
|
|
|
69
|
|
|
wb = Workbook() |
70
|
|
|
|
71
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
72
|
|
|
wb.save(filename) |
73
|
|
|
|
74
|
|
|
return filename |
75
|
|
|
|