feat(rohs-check): 新增 RoHS 对话轮次注册与检核批次绑定功能
This commit is contained in:
1 parent
9cba517bff
commit
fa0cdee8df
20 files changed
+1050
-201
No files matched your search
@@ -0,0 +1,58 @@
|
||||
"""
|
||||
backend/edkb_system/skills/excel_bridge.py
|
||||
|
||||
用途:Windows 环境下通过 Excel COM 将 .xls 转为 .xlsx。
|
||||
依赖:pywin32(仅 Windows 且已安装 Microsoft Excel 时可用)。
|
||||
逻辑:由 loader_excel._convert_xls_to_xlsx 在 soffice 失败后按需调用。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def convert_with_excel(path: Path, output: Path) -> Path:
|
||||
"""
|
||||
使用 Microsoft Excel COM 将源表格另存为 .xlsx。
|
||||
|
||||
核心逻辑:启动 Excel.Application → Open → SaveAs(xlsx) → Quit。
|
||||
|
||||
Args:
|
||||
path: 源 .xls 文件路径。
|
||||
output: 目标 .xlsx 路径。
|
||||
|
||||
Returns:
|
||||
成功写入的 output 路径。
|
||||
|
||||
Raises:
|
||||
RuntimeError: 非 Windows、缺少 pywin32/Excel,或转换未产出目标文件。
|
||||
"""
|
||||
import os
|
||||
|
||||
if os.name != "nt":
|
||||
raise RuntimeError("Excel COM conversion is only available on Windows")
|
||||
|
||||
try:
|
||||
import win32com.client
|
||||
except ImportError as exc:
|
||||
raise RuntimeError("Excel COM conversion requires pywin32 on Windows") from exc
|
||||
|
||||
source = str(path.resolve())
|
||||
target = str(output.resolve())
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
excel = win32com.client.Dispatch("Excel.Application")
|
||||
excel.Visible = False
|
||||
excel.DisplayAlerts = False
|
||||
workbook = None
|
||||
try:
|
||||
workbook = excel.Workbooks.Open(source)
|
||||
# 51 = xlOpenXMLWorkbook (.xlsx)
|
||||
workbook.SaveAs(target, FileFormat=51)
|
||||
finally:
|
||||
if workbook is not None:
|
||||
workbook.Close(SaveChanges=False)
|
||||
excel.Quit()
|
||||
|
||||
if not output.exists():
|
||||
raise RuntimeError(f"Excel COM did not produce output file: {output}")
|
||||
return output
|
||||
@@ -7,13 +7,13 @@ backend/edkb_system/skills/loader_excel.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from openpyxl import load_workbook, Workbook
|
||||
|
||||
from edkb_system.contracts import Context
|
||||
from edkb_system.skills.soffice_util import convert_with_soffice, soffice_candidates
|
||||
from edkb_system.skills.excel_bridge import convert_with_excel
|
||||
|
||||
|
||||
def _convert_xls_to_xlsx_python(path: Path, output: Path) -> Path:
|
||||
@@ -64,6 +64,8 @@ def _convert_xls_to_xlsx(path: Path, out_dir: Path) -> Path:
|
||||
# 2. Windows 环境下尝试 Excel COM
|
||||
if os.name == "nt":
|
||||
try:
|
||||
from edkb_system.skills.excel_bridge import convert_with_excel
|
||||
|
||||
return convert_with_excel(path, output)
|
||||
except Exception as exc:
|
||||
errors.append(f"Excel COM conversion failed: {exc}")
|
||||
@@ -93,7 +95,6 @@ def _trim_trailing_empty(vals: list[str]) -> list[str]:
|
||||
|
||||
|
||||
def run(input_data: dict, context: Context) -> list[dict]:
|
||||
import os
|
||||
file_path = Path(input_data["file_path"])
|
||||
work_dir = Path(input_data["work_dir"])
|
||||
if file_path.suffix.lower() == ".xls":
|
||||
|
||||
Reference in new issue
Block a user