Update src/streamlit_app.py
Browse files294 unmodified lines295296297298298299300301302303304305306307308305306307309310311312310313314315316433 unmodified lines750751752750753754752753754755756757758759760761762755756757758759760294 unmodified lines return {} finally: # 清理临时文件 shutil.rmtree(temp_dir) try: if 'temp_file' in locals() and os.path.exists(temp_file): os.remove(temp_file) except Exception as e: pass # 忽略任何清理错误
uploaded_file = st.file_uploader("上传Excel文件(包含ASIN及产品信息的数据表)", type=["xlsx"], help="请上传包含ASIN列的Excel文件,文件应包含产品信息如品牌、价格、销量等数据。系统将自动提取嵌入的产品图片。")
if uploaded_file is not None: try: # 使用唯一文件名避免冲突 unique_id = str(uuid.uuid4()) temp_file = f"temp_upload_{unique_id}.xlsx" # 使用临时文件系统创建临时文件 temp_fd, temp_file = tempfile.mkstemp(suffix=".xlsx") # 保存上传的文件到临时位置 with open(temp_file, "wb") as f: with os.fdopen(temp_fd, "wb") as f: f.write(uploaded_file.getvalue()) # 读取Excel文件433 unmodified lines st.error(f"读取Excel文件时出错: {str(e)}") st.info("请确保上传的Excel文件格式正确且未损坏") finally: # 清理临时文件 - 使用延迟和尝试多次删除 # 清理临时文件 try: if os.path.exists(temp_file): try: os.remove(temp_file) except PermissionError: # 如果文件被占用,等待一会儿再尝试 time.sleep(1) try: os.remove(temp_file) except: pass # 忽略最终的错误 except: if 'temp_file' in locals() and os.path.exists(temp_file): os.remove(temp_file) except Exception as e: pass # 忽略任何清理错误else: st.info("请上传Excel文件以开始提取数据")src/streamlit_app.py+12-17