Please help me, the programmer who made this software is gone and now I’m in trouble: the program opens an xlsx file, inserts some data inside and saves it with another name, I need this file to be saved in a specific directory, can anyone help me? Thank you!
JavaScript
x
135
135
1
import datetime
2
import os
3
from shutil import copyfile
4
import xml.etree.ElementTree as ET
5
from openpyxl import load_workbook
6
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
7
8
class MeasurementsData:
9
def __init__(self):
10
self.tag = ""
11
self.date = ""
12
self.filename = ""
13
self.application = ""
14
self.element = ""
15
self.concentration = ""
16
self.unit = ""
17
self.concentration_results = []
18
19
def parse_empirical_assay(self, result):
20
application_methods=result.find('EmpiricalAssayResult').find('ScreeningInfo').findall('ScreeningMethod')
21
for method in application_methods:
22
if self.application != "":
23
self.application += " - "
24
self.application += method.attrib['name']
25
26
concentration_results = result.find('EmpiricalAssayResult').find('ConcentrationResults').findall('ConcentrationResult')
27
for concentration_result in concentration_results:
28
self.concentration_results.append({
29
'element': concentration_result.attrib['name'],
30
'concentration': concentration_result.attrib['concentration'],
31
'unit': concentration_result.attrib['unit']
32
})
33
34
def parse_fundamental_parameters(self, result):
35
application_methods=result.find('FundamentalParametersResult').find('ScreeningInfo').findall('ScreeningMethod')
36
for method in application_methods:
37
if self.application != "":
38
self.application += " - "
39
self.application += method.attrib['name']
40
41
concentration_results = result.find('FundamentalParametersResult').find('ConcentrationResults').findall('ConcentrationResult')
42
for concentration_result in concentration_results:
43
self.concentration_results.append({
44
'element': concentration_result.attrib['name'],
45
'concentration': concentration_result.attrib['concentration'],
46
'unit': concentration_result.attrib['unit']
47
})
48
49
def parse(self, data):
50
#print(data)
51
xml_data = ET.fromstring(data)
52
self.tag = xml_data.attrib['name']
53
if self.tag == None or self.tag == "":
54
self.tag = "noname"
55
self.filename = self.tag + ".xlsx"
56
self.date = xml_data.attrib['measurementTime']
57
self.date = self.date.split('.')[0]
58
self.date=datetime.datetime.strptime(self.date, '%Y-%m-%dT%H:%M:%S')
59
#print(self.tag)
60
61
result = xml_data.find('Result')
62
if result[0].tag == "EmpiricalAssayResult":
63
if not os.path.isfile(self.filename):
64
copyfile("files/ReportDiMisura1.xlsx", self.filename)
65
self.parse_empirical_assay(result)
66
elif result[0].tag == "FundamentalParametersResult":
67
if not os.path.isfile(self.filename):
68
copyfile("files/ReportDiMisura9Colonne2.xlsx", self.filename)
69
self.parse_fundamental_parameters(result)
70
71
wb = load_workbook(filename = self.filename)
72
if wb == None:
73
print('File xlsm non trovato!')
74
return
75
76
ws = wb['Report']
77
if wb == None:
78
print('Foglio Report non presente!')
79
return
80
81
font = Font(name='Calibri',
82
size=11,
83
bold=False,
84
italic=False,
85
vertAlign=None,
86
underline='none',
87
strike=False,
88
color='FF000000')
89
90
alignment = Alignment(horizontal='center',
91
vertical='center',
92
text_rotation=0,
93
wrap_text=False,
94
shrink_to_fit=False,
95
indent=0)
96
97
cell = ws.cell(row=10, column=2)
98
cell.value = self.application
99
cell.font = font
100
cell.alignment = alignment
101
102
headers_row=23
103
row = headers_row+1
104
count=1
105
cell = ws.cell(row=row, column=1)
106
while cell.value != None:
107
row += 1
108
count += 1
109
cell = ws.cell(row=row, column=1)
110
111
if cell.value == None:
112
cell.value = self.date #.strftime("%d/%m/%Y %H:%M")
113
cell.font = font
114
cell.alignment = alignment
115
116
cell = ws.cell(row=row, column=2)
117
cell.value = count
118
cell.font = font
119
cell.alignment = alignment
120
121
column=3
122
for concentration_result in self.concentration_results:
123
cell = ws.cell(row=headers_row, column=column)
124
cell.value = concentration_result['element'] + " " + concentration_result['unit']
125
cell.font = font
126
cell.alignment = alignment
127
128
cell = ws.cell(row=row, column=column)
129
cell.value = float(concentration_result['concentration'])
130
cell.font = font
131
cell.alignment = alignment
132
column+=1
133
134
wb.save (filename = self.filename)
135
Advertisement
Answer
In wb.save (filename = self.filename)
you can provide any path you need in place of self.filename
.
The filename that it gets is defined in the parse method, specifically on following lines:
JavaScript
1
5
1
self.tag = xml_data.attrib['name']
2
if self.tag == None or self.tag == "":
3
self.tag = "noname"
4
self.filename = self.tag + ".xlsx"
5
From there, you can prepend the contents of self.filename
with the desired path. Like this for example:
JavaScript
1
2
1
self.filename = "/desired/path/" + self.tag + ".xlsx" # On Linux
2
or
JavaScript
1
2
1
self.filename = "C:/desired/path/" + self.tag + ".xlsx" # On Windows
2