xlsx output

This commit is contained in:
Thaddeus Hughes
2025-10-09 08:45:36 -05:00
parent deea966f6c
commit 948f9363c7
6 changed files with 57 additions and 1 deletions

View File

@@ -0,0 +1 @@
,DESKTOP-M40GG9U/Thad,DESKTOP-M40GG9U,09.10.2025 08:10,file:///C:/Users/Thad/AppData/Roaming/LibreOffice/4;

1
.~lock.LogAB(20).xls# Normal file
View File

@@ -0,0 +1 @@
,DESKTOP-M40GG9U/Thad,DESKTOP-M40GG9U,09.10.2025 08:40,file:///C:/Users/Thad/AppData/Roaming/LibreOffice/4;

BIN
LogAB(20).xls Normal file

Binary file not shown.

BIN
LogAB(20).xlsx Normal file

Binary file not shown.

56
main.py
View File

@@ -5,6 +5,12 @@ from tkinter import filedialog, messagebox
import subprocess import subprocess
import os import os
from datetime import datetime from datetime import datetime
import shutil
try:
from openpyxl import load_workbook
XLSX_AVAILABLE = True
except ImportError:
XLSX_AVAILABLE = False
def parse_logs(log_text): def parse_logs(log_text):
lines = log_text.splitlines() lines = log_text.splitlines()
@@ -72,6 +78,42 @@ def write_csv2(pairs, filename):
gross_weight, gross_units, gross_time = pairs[-1] gross_weight, gross_units, gross_time = pairs[-1]
writer.writerow([gross_weight, '', '', gross_time, '', gross_units, '', '']) writer.writerow([gross_weight, '', '', gross_time, '', gross_units, '', ''])
def write_xlsx(pairs, output_filename, template_path='template.xlsx'):
"""Write sequential data to XLSX file using template"""
if not XLSX_AVAILABLE:
raise ImportError("openpyxl is required for XLSX export. Install with: pip install openpyxl")
if not os.path.exists(template_path):
raise FileNotFoundError(f"Template file not found: {template_path}")
# Load the template workbook (data_only=False preserves formulas)
wb = load_workbook(template_path, data_only=False)
# Check if SEQUENTIAL sheet exists
if 'SEQUENTIAL' not in wb.sheetnames:
raise ValueError("Template does not contain a 'SEQUENTIAL' sheet")
# Get the SEQUENTIAL sheet
ws = wb['SEQUENTIAL']
# Clear existing data (starting from row 2, assuming row 1 has headers)
# First, delete all rows below the header
if ws.max_row > 1:
ws.delete_rows(2, ws.max_row - 1)
# Write new data starting from row 2
for row_idx, (weight, units, timestamp) in enumerate(pairs, start=2):
ws.cell(row=row_idx, column=1, value=weight)
ws.cell(row=row_idx, column=2, value=units)
ws.cell(row=row_idx, column=3, value=timestamp)
# Set COMBINED as the active sheet (default sheet when opened)
if 'COMBINED' in wb.sheetnames:
wb.active = wb.sheetnames.index('COMBINED')
# Save to new filename (never overwrite template)
wb.save(output_filename)
def run_update_script(): def run_update_script():
try: try:
# Run update.sh script # Run update.sh script
@@ -101,6 +143,7 @@ def process_files():
base = os.path.splitext(input_file)[0] base = os.path.splitext(input_file)[0]
output_file1 = f"{base}.sequential.csv" output_file1 = f"{base}.sequential.csv"
output_file2 = f"{base}.joined.csv" output_file2 = f"{base}.joined.csv"
output_xlsx = f"{base}.xlsx"
try: try:
with open(input_file, 'r') as f: with open(input_file, 'r') as f:
@@ -108,7 +151,18 @@ def process_files():
pairs = parse_logs(log_text) pairs = parse_logs(log_text)
write_csv1(pairs, output_file1) write_csv1(pairs, output_file1)
write_csv2(pairs, output_file2) write_csv2(pairs, output_file2)
messagebox.showinfo("Success", f"CSV files generated:\n{output_file1}\n{output_file2}")
# Try to write XLSX file
xlsx_msg = ""
try:
write_xlsx(pairs, output_xlsx)
xlsx_msg = f"\n{output_xlsx}"
except ImportError as e:
xlsx_msg = f"\n\nXLSX export skipped: {str(e)}"
except Exception as e:
xlsx_msg = f"\n\nXLSX export failed: {str(e)}"
messagebox.showinfo("Success", f"Files generated:\n{output_file1}\n{output_file2}{xlsx_msg}")
except Exception as e: except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}") messagebox.showerror("Error", f"An error occurred: {str(e)}")

BIN
template.xlsx Normal file

Binary file not shown.