-
Notifications
You must be signed in to change notification settings - Fork 2
/
excel.py
106 lines (83 loc) · 4.84 KB
/
excel.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
import os
import datetime
import xlsxwriter
from iata import IataCodes
class ExcelWriter:
def __init__(self):
self.__iatacodes = IataCodes()
self.__current_dir = os.path.dirname(__file__)
def write_to_excel(self, data):
if len(data[0]) == 11:
headers = ["Preis", "Start Flughafen (IATA)", "Start Stadt", "Ziel Flughafen (IATA)",
"Ziel Stadt", "Flugdatum", "Abflug Uhrzeit", "Ankunft Uhrzeit", "Reisezeit", "Stopps", "Link"]
else:
headers = ["Preis", "Preis pro Person", "Start Flughafen (IATA)", "Start Stadt", "Ziel Flughafen (IATA)",
"Ziel Stadt", "Flugdatum", "Abflug Uhrzeit", "Ankunft Uhrzeit", "Reisezeit", "Stopps", "Link"]
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
workbook = xlsxwriter.Workbook(
os.path.join(self.__current_dir, f"./output/flightlists/{timestamp}_flightsearch.xlsx"))
worksheet = workbook.add_worksheet("Deine Ergebnisse")
header = workbook.add_format({'bold': True})
header.set_align("center")
# header.set_font_size(16)
money = workbook.add_format({"num_format": "#.##0,0 €"})
normal = workbook.add_format().set_align("vleft")
for i_column in range(len(headers)):
worksheet.write(0, i_column, headers[i_column], header)
for i_row in range(1, len(data) + 1):
for i_column in range(len(data[0])):
if isinstance(data[i_row - 1][i_column], float):
worksheet.write(i_row, i_column, data[i_row - 1][i_column], money)
else:
if i_column in (headers.index("Start Stadt"), headers.index("Ziel Stadt")):
print(i_column, headers.index("Start Stadt"), headers.index("Ziel Stadt"))
worksheet.write(i_row, i_column,
self.__iatacodes.getCityByIataCode(data[i_row - 1][i_column - 1]))
else:
worksheet.write(i_row, i_column, data[i_row - 1][i_column])
worksheet.autofit()
workbook.close()
def return_write_to_excel(self, data):
if len(data[0]) == 20:
one_person = True
headers = ["Preis", "Start Flughafen (IATA)", "Start Stadt", "Ziel Flughafen (IATA)",
"Ziel Stadt", "Flugdatum", "Abflug Uhrzeit", "Ankunft Uhrzeit", "Reisezeit", "Stopps",
"", "Start Flughafen (IATA)", "Start Stadt", "Ziel Flughafen (IATA)",
"Ziel Stadt", "Flugdatum", "Abflug Uhrzeit", "Ankunft Uhrzeit", "Reisezeit", "Stopps",
"Link"]
else:
one_person = False
headers = ["Preis", "Preis pro Person", "Start Flughafen (IATA)", "Start Stadt", "Ziel Flughafen (IATA)",
"Ziel Stadt", "Flugdatum", "Abflug Uhrzeit", "Ankunft Uhrzeit", "Reisezeit", "Stopps",
"", "Start Flughafen (IATA)", "Start Stadt", "Ziel Flughafen (IATA)",
"Ziel Stadt", "Flugdatum", "Abflug Uhrzeit", "Ankunft Uhrzeit", "Reisezeit", "Stopps",
"Link"]
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
workbook = xlsxwriter.Workbook(
os.path.join(self.__current_dir, f"./output/flightlists/{timestamp}_flightsearch.xlsx"))
worksheet = workbook.add_worksheet("Deine Ergebnisse")
header = workbook.add_format({'bold': True})
header.set_align("center")
# header.set_font_size(16)
money = workbook.add_format({"num_format": "#.##0,0 €"})
normal = workbook.add_format().set_align("vleft")
for i_column in range(len(headers)):
worksheet.write(0, i_column, headers[i_column], header)
for i_row in range(1, len(data) + 1):
if one_person:
data[i_row - 1].insert(10, "Rückflug")
else:
data[i_row - 1].insert(11, "Rückflug")
for i_column in range(len(data[0])):
if isinstance(data[i_row - 1][i_column], float):
worksheet.write(i_row, i_column, data[i_row - 1][i_column], money)
else:
if i_column in (
headers.index("Start Stadt"), headers.index("Ziel Stadt"), headers.index("Start Stadt", 11),
headers.index("Ziel Stadt", 11)):
worksheet.write(i_row, i_column,
self.__iatacodes.getCityByIataCode(data[i_row - 1][i_column - 1]))
else:
worksheet.write(i_row, i_column, data[i_row - 1][i_column])
worksheet.autofit()
workbook.close()