0
我得到的任務是解析一個XML腳本以獲得某些值到Excel工作表中。 該腳本運行良好,但現在我必須創建一個可執行文件。pyinstaller創建一個無用的可執行文件
當我讀到它的一些事情後,我認爲pyinstaller可能是達到我的目標的最佳方式。但它只會產生一個無用的可執行文件,它什麼也不做。
所以我讀了更多,發現,python使用隱藏的庫。我試圖發現,我的藏書庫究竟是什麼以及如何包含它們,但是我沒有這麼做。
我被告知使用「-v」選項來獲取隱藏的庫文件。但我真的不知道,我在看什麼。
如果我嘗試將某些東西寫入spec文件中的「hiddenimports」,但它被覆蓋。 所以我不完全確定它是否被使用。
我附上了代碼,它應該都是標準庫文件。
有人能請我指點我失蹤的東西嗎?
謝謝!
from xml.etree import ElementTree
import openpyxl
import tkinter as tk
from tkinter import filedialog
import time
def getFileName(filepath):
print("File Path: {}".format(filepath))
split1 = filepath.split(".")
split2 = split1[0].split("/")
print("Filename: {}".format(split2[-1]))
return split2[-1]
def parseFile(input_file):
xmlTree = ElementTree.parse(input_file)
xmlRoot = xmlTree.getroot()
filename = getFileName(input_file)
comm = xmlRoot.find('vehicle/communications')
ECUS_events = comm.find("ecus[@type='read_events']")
ECUS_diag = comm.find("ecus[@type='single_ecu_diagnostic']")
ECUS_meas = comm.find("ecus[@type='read_measurements']")
print("ECUS: 'Read_Events':{}, 'Read_Measurements':{}, 'Single_Diagnostic':{}".format((ECUS_events is not None),
(ECUS_meas is not None),
(ECUS_diag is not None)))
print("ECUS: {}, {}, {}\n".format(ECUS_events, ECUS_meas, ECUS_diag))
ecus = []
dtc = []
for ECU in ECUS_events.iter('ecu'):
ecu = []
ecu_name = ECU.find('ecu_name').text
ecu_id = ECU.find('ecu_id').text
print("ECU: {} - {}".format(ecu_id, ecu_name))
ecu.append(ecu_name)
ecu.append(ecu_id)
ecu_master_ident = ECU.find("ecu_master[@type='ident']")
if ecu_master_ident is not None:
for value in ecu_master_ident.iter('values'):
if value.find('display_name') is not None:
if value.find('display_name').text == "SoftwareVersion":
SW_version = value.find('display_value').text
print("Found SW ver: {}".format(SW_version))
ecu.append(SW_version)
if value.find('display_name') is not None:
if value.find('display_name').text == "HardwareVersion":
HW_version = value.find('display_value').text
print("Found HW ver: {}".format(HW_version))
ecu.append(HW_version)
if value.find('display_name') is not None:
if value.find('display_name').text == "VW/Audi part number":
VW_AUDI_part = value.find('display_value').text
print("Found VW/Audi #: {}".format(VW_AUDI_part))
ecu.append(VW_AUDI_part)
if value.find('display_name') is not None:
if value.find('display_name').text == "Hardware part number":
HW_part = value.find('display_value').text
print("Found HW #: {}".format(HW_part))
ecu.append(HW_part)
ecu_master_dtc = ECU.find("ecu_master[@type='event_memory']")
DTC_count = 0
if ecu_master_dtc is not None:
DTCs = ecu_master_dtc.findall('values')
for DTC in DTCs:
DTC_count += 1
DTC_name = DTC.find('display_name')
DTC_number = DTC.find('fault_number')
DTC_text = DTC.find('dtc_text')
DTC_ecu = ecu_id + " - " + ecu_name
print("DTC: {} - {} | {}".format(DTC_number.text, DTC_name.text, DTC_text.text))
dtc.append([DTC_number.text, DTC_name.text, DTC_text.text, DTC_ecu])
if DTC_count:
print("DTC Count: {}".format(DTC_count))
else:
print("No DTC Category")
ecu.append(DTC_count)
print("\n")
ecus.append(ecu)
single_diag = []
if ECUS_diag is not None:
for ECU in ECUS_diag.iter('ecu'):
ecu_info = []
ecu_name = ECU.find('ecu_name').text
ecu_id = ECU.find('ecu_id').text
length = len(ecu_name) + len(ecu_id) + 18
for char in range(0, length):
print("-", end="")
print("\n---- ECU: {} - {} ----".format(ecu_id, ecu_name))
for char in range(0, length):
print("-", end="")
print("")
ecu_info.append(ecu_name)
ecu_info.append(ecu_id)
ecu_master_ident = ECU.find("ecu_master[@type='ident']")
if ecu_master_ident is not None:
for value in ecu_master_ident.iter('values'):
if value.find('display_name') is not None:
if value.find('display_name').text == "Coding":
coding = value.find('display_value').text
print("Found Coding: {}".format(coding))
ecu_info.append(coding)
ecu_master_adapt = ECU.find("ecu_master[@type='adaption_read']")
ecu_adaption = []
if ecu_master_adapt is not None:
print("Gathering 'Adaption' data...")
for values in ecu_master_adapt.findall('values'):
adapt_list = []
Outer_cat = values.find('display_name')
adapt_list.append(Outer_cat.text)
value_list = []
for value in values.findall('values'):
inner_name = value.find('display_name')
inner_val = value.find('display_value')
if inner_name is not None and inner_val is not None:
value_list.append([inner_name.text, inner_val.text])
elif inner_val is not None:
value_list.append([inner_val.text])
elif inner_name is not None:
value_list.append([inner_name.text])
else:
value_list.append([])
adapt_list.append(value_list)
ecu_adaption.append(adapt_list)
ecu_master_coding = ECU.find("ecu_master[@type='coding_read']")
ecu_coding = []
if ecu_master_coding is not None:
print("Gathering 'Coding' data...")
for values in ecu_master_coding.findall('values'):
code_list = []
val_list = []
code_name = values.find('display_name')
code_val = values.find('display_value')
code_bin = values.find('bin_value')
code_hex = values.find('hex_value')
if code_name is not None:
code_list.append(code_name.text)
else:
print("No Code Name present")
code_list.append("None")
if code_val is not None:
val_list.append(code_val.text)
if code_bin is not None:
val_list.append(["Hex", code_bin.text])
if code_bin is not None:
val_list.append(["Bin", code_hex.text])
code_list.append(val_list)
# print(code_list)
ecu_coding.append(code_list)
single_diag.append([ecu_info, ecu_adaption, ecu_coding])
meas_ocu = []
if ECUS_meas is not None:
for ECU in ECUS_meas.iter('ecu'):
ecu_name = ECU.find('ecu_name').text
ecu_id = ECU.find('ecu_id').text
if ecu_name == "Telematics Communication Unit" and ecu_id == "0075":
ocu_info = [ecu_name, ecu_id]
length = len(ecu_name) + len(ecu_id) + 18
for char in range(0, length):
print("-", end="")
print("\n---- OCU: {} - {} ----".format(ecu_id, ecu_name))
for char in range(0, length):
print("-", end="")
print("")
ocu_master = ECU.find("ecu_master[@type='measurement']")
ocu_values = []
for Values in ocu_master.findall('values'):
display_name = Values.find('display_name')
if display_name is not None:
print(display_name.text)
values_disp = []
for values in Values.findall('values'):
disp_value = values.find('display_value')
disp_unit = values.find('display_unit')
disp_name = values.find('display_name')
if disp_value is not None and disp_unit is not None and disp_name is not None:
print("Name: {} | Value: {} {}".format(disp_name.text, disp_value.text, disp_unit.text))
values_disp.append([disp_name.text, "{} {}".format(disp_value.text, disp_unit.text)])
elif disp_value is not None and disp_name is not None:
print("Name: {} | Value: {}".format(disp_name.text, disp_value.text))
values_disp.append([disp_name.text, disp_value.text])
elif disp_value is not None and disp_unit is not None:
print("Value: {} {}".format(disp_value.text, disp_unit.text))
values_disp.append(["{} {}".format(disp_value.text, disp_unit.text)])
elif disp_value is not None:
print("Value: {}".format(disp_value.text))
values_disp.append([disp_value.text])
ocu_values.append([display_name.text, values_disp])
print("")
meas_ocu.append([[ecu_name, ecu_id], ocu_values])
print("\n")
print("Printing final structures:\n")
print("ECU Readouts:")
for elem in ecus:
print(elem)
print("\nDTC Readouts:")
for elem in dtc:
print(elem)
book = openpyxl.Workbook()
sheet_ecu = book.active
sheet_ecu.title = 'ECUs'
sheet_dtc = book.create_sheet("DTCs", 1)
sheet_0075 = book.create_sheet("ECU - 0075", 2)
sheet_005F = book.create_sheet("ECU - 005F", 3)
print("\nWriting to workbook...")
sheet_ecu.cell(row=1, column=1, value="ECU Name")
sheet_ecu.cell(row=1, column=2, value="ECU ID")
sheet_ecu.cell(row=1, column=3, value="SW")
sheet_ecu.cell(row=1, column=4, value="HW")
sheet_ecu.cell(row=1, column=5, value="VW/Audi #")
sheet_ecu.cell(row=1, column=6, value="HW #")
sheet_ecu.cell(row=1, column=7, value="DTCs")
sheet_dtc.cell(row=1, column=1, value="DTC#")
sheet_dtc.cell(row=1, column=2, value="Name")
sheet_dtc.cell(row=1, column=3, value="Text")
sheet_dtc.cell(row=1, column=4, value="ECU")
sheet_005F.cell(row=1, column=1, value="No '005F' diagnostic found")
sheet_0075.cell(row=1, column=1, value="No '0075' diagnostic found")
cur_row = 2
for elem in ecus:
cur_column = 1
for cell in elem:
sheet_ecu.cell(row=cur_row, column=cur_column, value=cell)
cur_column += 1
cur_row += 1
cur_row = 2
for elem in dtc:
cur_column = 1
for cell in elem:
sheet_dtc.cell(row=cur_row, column=cur_column, value=cell)
cur_column += 1
cur_row += 1
telecom_cur_row = 0
if len(single_diag) > 0:
print("Single Diag: {} {}".format(len(single_diag), [single_diag[0][0][1], single_diag[1][0][1]]))
for diag in single_diag:
print("ECU Diagnostic:{} - {}".format(diag[0][1], diag[0][0]))
if diag[0][1] == "0075":
sheet_0075.cell(row=1, column=1, value=diag[0][1])
sheet_0075.cell(row=1, column=2, value=diag[0][0])
sheet_0075.cell(row=2, column=1, value="Coding")
sheet_0075.cell(row=2, column=2, value=diag[0][2])
sheet_0075.cell(row=4, column=1, value="--Adaption--")
cur_row = 5
for element in diag[1]:
sheet_0075.cell(row=cur_row, column=1, value=element[0])
if len(element[1]) > 1:
string = ""
for value in element[1]:
string += "{} : {}".format(value[0], value[1]) + " \n"
else:
string = element[1]
while type(string) is list:
string = string[0]
sheet_0075.cell(row=cur_row, column=2, value=string)
cur_row += 1
sheet_0075.cell(row=cur_row + 1, column=1, value="--Coding--")
cur_row += 2
for element in diag[2]:
sheet_0075.cell(row=cur_row, column=1, value=element[0])
if len(element[1]) > 1:
string = ""
for value in element[1]:
string += "{} : {}".format(value[0], value[1]) + " \n"
else:
string = element[1]
while type(string) is list:
string = string[0]
sheet_0075.cell(row=cur_row, column=2, value=string)
cur_row += 1
telecom_cur_row = cur_row + 1
if diag[0][1] == "005F":
sheet_005F.cell(row=1, column=1, value=diag[0][1])
sheet_005F.cell(row=1, column=2, value=diag[0][0])
sheet_005F.cell(row=2, column=1, value="Coding")
sheet_005F.cell(row=2, column=2, value=diag[0][2])
sheet_005F.cell(row=4, column=1, value="--Adaption--")
cur_row = 5
for element in diag[1]:
sheet_005F.cell(row=cur_row, column=1, value=element[0])
if len(element[1]) > 1:
string = ""
for value in element[1]:
string += "{} : {}".format(value[0], value[1]) + " \n"
else:
string = element[0]
while type(string) is list:
string = string[0]
sheet_005F.cell(row=cur_row, column=2, value=string)
cur_row += 1
sheet_005F.cell(row=cur_row + 1, column=1, value="--Coding--")
cur_row += 2
for element in diag[2]:
sheet_005F.cell(row=cur_row, column=1, value=element[0])
if len(element[1]) > 1:
string = ""
for value in element[1]:
string += "{} : {}".format(value[0], value[1]) + " \n"
else:
string = element[1]
while type(string) is list:
string = string[0]
sheet_005F.cell(row=cur_row, column=2, value=string)
cur_row += 1
if len(meas_ocu) > 0:
# print(meas_ocu)
if telecom_cur_row == 0:
telecom_cur_row = 3
for diag in meas_ocu:
cur_row = telecom_cur_row
print("OCU Diagnostic: {} - {}".format(diag[0][0], diag[0][1]))
if diag[0][1] == "0075":
sheet_0075.cell(row=cur_row, column=1, value="--Measured Values--")
cur_row += 1
for element in diag[1]:
sheet_0075.cell(row=cur_row, column=1, value=element[0])
if len(element[1]) > 1:
string = ""
for value in element[1]:
string += "{} : {}".format(value[0], value[1]) + " \n"
elif len(element[1]) == 1:
string = element[1]
while type(string) is list:
string = string[0]
sheet_0075.cell(row=cur_row, column=2, value=string)
cur_row += 1
print("Saving to '.xlsx' file....")
book.save("{}_parsed.xlsx".format(filename))
print("Complete")
def verifyFile(filename):
approve = 1
print("File Path: {}".format(filename))
split1 = filename.split(".")
if split1[1] == "xml":
print("File Extension Match!")
else:
approve = 0
print("File Extension '{}' Incorrect, rejecting".format(split1[1]))
print("")
return approve
def buttonPrompt():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
print(file_path)
if verifyFile(file_path):
parseFile(file_path)
else:
print("File Rejected")
if __name__ == '__main__':
buttonPrompt()
不知道它是否與你的問題有關,但openpyxl不在標準庫中。 – lenz