Spaces:
Configuration error
Configuration error
Commit
·
8ccb7de
1
Parent(s):
846aae3
quick
Browse files
main.py
CHANGED
|
@@ -2,19 +2,22 @@ import pandas as pd
|
|
| 2 |
from openpyxl import load_workbook
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
import os # Added for path manipulation
|
|
|
|
| 5 |
|
| 6 |
def process_files(file1_path, file2_path, output_filename="generated_report.xlsx"):
|
| 7 |
"""
|
| 8 |
Processes two input Excel files and generates a combined report.
|
|
|
|
| 9 |
|
| 10 |
Args:
|
| 11 |
file1_path (str): Path to the first input Excel file (data source).
|
| 12 |
file2_path (str): Path to the second input Excel file (template).
|
| 13 |
-
output_filename (str): Desired name for the output report file.
|
|
|
|
| 14 |
|
| 15 |
Returns:
|
| 16 |
-
str: The path to the generated output Excel file.
|
| 17 |
-
|
| 18 |
"""
|
| 19 |
try:
|
| 20 |
# 读取第一个文件
|
|
@@ -149,9 +152,26 @@ def process_files(file1_path, file2_path, output_filename="generated_report.xlsx
|
|
| 149 |
if 'WACKER' in wb.sheetnames:
|
| 150 |
del wb['WACKER'] # Remove template if no longer needed
|
| 151 |
|
| 152 |
-
#
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
except FileNotFoundError:
|
| 157 |
print(f"Error: Input file not found. Check paths: {file1_path}, {file2_path}")
|
|
|
|
| 2 |
from openpyxl import load_workbook
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
import os # Added for path manipulation
|
| 5 |
+
import tempfile # Import tempfile
|
| 6 |
|
| 7 |
def process_files(file1_path, file2_path, output_filename="generated_report.xlsx"):
|
| 8 |
"""
|
| 9 |
Processes two input Excel files and generates a combined report.
|
| 10 |
+
Saves the output to a temporary directory to avoid potential permission issues.
|
| 11 |
|
| 12 |
Args:
|
| 13 |
file1_path (str): Path to the first input Excel file (data source).
|
| 14 |
file2_path (str): Path to the second input Excel file (template).
|
| 15 |
+
output_filename (str): Desired *base* name for the output report file (e.g., 'report.xlsx').
|
| 16 |
+
The actual path will be in a temp directory.
|
| 17 |
|
| 18 |
Returns:
|
| 19 |
+
str: The full path to the generated output Excel file in a temporary directory.
|
| 20 |
+
Returns None if an error occurs during processing.
|
| 21 |
"""
|
| 22 |
try:
|
| 23 |
# 读取第一个文件
|
|
|
|
| 152 |
if 'WACKER' in wb.sheetnames:
|
| 153 |
del wb['WACKER'] # Remove template if no longer needed
|
| 154 |
|
| 155 |
+
# Create a temporary file path for the output
|
| 156 |
+
try:
|
| 157 |
+
temp_dir = tempfile.gettempdir()
|
| 158 |
+
# Ensure the base output filename is used, not a potentially problematic one from input args
|
| 159 |
+
safe_output_filename = os.path.basename(output_filename if output_filename else "generated_report.xlsx")
|
| 160 |
+
# Create a unique temporary file path
|
| 161 |
+
# Using mkstemp gives more control, but let's try simple join first
|
| 162 |
+
# _, temp_output_path = tempfile.mkstemp(suffix=".xlsx", prefix="report_", dir=temp_dir)
|
| 163 |
+
# Let's use a predictable name within the temp dir, might be easier for Gradio/platform
|
| 164 |
+
temp_output_path = os.path.join(temp_dir, safe_output_filename)
|
| 165 |
+
|
| 166 |
+
print(f"Attempting to save report to temporary path: {temp_output_path}")
|
| 167 |
+
wb.save(temp_output_path)
|
| 168 |
+
print(f"Successfully saved report to: {temp_output_path}")
|
| 169 |
+
return temp_output_path # Return the full path to the temporary file
|
| 170 |
+
except Exception as save_error:
|
| 171 |
+
print(f"Error saving workbook to temporary path {temp_output_path}: {save_error}")
|
| 172 |
+
import traceback
|
| 173 |
+
print(traceback.format_exc())
|
| 174 |
+
return None
|
| 175 |
|
| 176 |
except FileNotFoundError:
|
| 177 |
print(f"Error: Input file not found. Check paths: {file1_path}, {file2_path}")
|