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