DBsExcel_con_UDT/DB_to_Excel.py

49 lines
1.8 KiB
Python
Raw Permalink Normal View History

2024-04-23 04:52:24 -03:00
from CreateTableAndOffsets import convert_to_table
from ExpandDB import expand_dbs
from manejoArchivos import select_file, open_file_explorer
from FileSystem import *
from S7_DBParser import parse_dbs, parse_udts
from Excel import *
if __name__ == "__main__":
2024-04-23 04:52:24 -03:00
file_path = select_file()
if file_path: # Proceed only if a file was selected
with open(file_path, "r", encoding="utf-8-sig") as file:
lines = file.readlines()
2024-04-23 04:52:24 -03:00
file_name, extension, dest_path = extract_file_details(file_path)
dest_path = create_directory(dest_path, file_name)
json_path = build_file_path(dest_path, file_name, "json")
xml_path = build_file_path(dest_path, file_name, "xml")
excel_path = build_file_path(dest_path, file_name, "xlsx")
cvs_path = build_file_path(dest_path, file_name, "cvs")
udt_json = parse_udts(lines)
db_json = parse_dbs(lines, udt_json)
expand_dbs(udt_json, db_json) # Expand DBs with UDT definitions
2024-04-23 04:52:24 -03:00
save_data_as_json(db_json, json_path)
# Display the expanded DBs as a table
2024-04-23 04:52:24 -03:00
df = convert_to_table(db_json)
save_dataframe_to_file(df, cvs_path) # Save the DataFrame to a CSV file
## EXCEL output
save_dataframe_to_excel(
df, excel_path, file_name
) # Optionally, save the DataFrame to an Excel file
workbook, worksheet = open_worksheet(excel_path, file_name)
autosize_columns(worksheet, ["A", "B", "C", "D", "E", "F", "G"])
center_columns(worksheet, ["C", "D", "E"])
save_workbook(workbook, excel_path)
# Save JSON data to an XML file
2024-04-23 04:52:24 -03:00
save_json_to_xml(db_json, xml_path)
# Open the directory containing the new file in Explorer
2024-04-23 04:52:24 -03:00
open_file_explorer(os.path.dirname(excel_path))
else:
print("No file was selected.")