Files
37duAutomation/tests/wps.py
T
puddy 3e76d3d234 fix: 修复加班校验逻辑与OA单据重复问题,新增Excel重算工具
1.  加班校验不通过时直接返回驳回结果,不再执行后续校验
2.  为OA待办查询接口添加去重逻辑,避免重复审批单据
3.  新增WPS COM调用的Excel公式重算测试工具
2026-08-12 17:47:49 +08:00

57 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import win32com.client
from openpyxl import load_workbook
import os
def recalc_excel_by_wps_com(file_path: str):
"""
使用WPS COM重算表格,保存计算缓存到文件
仅支持 WPS专业版 / MS‑Excel
"""
abs_path = os.path.abspath(file_path)
app = None
try:
# 尝试WPS表格 COM
app = win32com.client.gencache.EnsureDispatch("Ket.Application")
app.Visible = False # 后台不显示窗口
app.DisplayAlerts = False # 关闭弹窗警告
# UpdateLinks=2 更新外部链接;你是本文件公式也带上无妨
wb = app.Workbooks.Open(abs_path, UpdateLinks=2)
# 强制完整重算全部公式
wb.Application.CalculateFullRebuild()
# 关键:保存,把计算结果缓存写入xlsx磁盘
wb.Save()
wb.Close()
except Exception as e:
print(f"COM执行异常: {e}")
raise
finally:
if app is not None:
app.Quit()
del app
if __name__ == "__main__":
file = r"C:\Users\admin\Desktop\test.xlsx"
# ==========1. openpyxl写入测试数据(模拟粘贴数值到目标表)==========
wb = load_workbook(file)
ws = wb.active
ws["A2"] = 10
ws["B2"] = 20
# C2单元格预先在文件内写好公式 =A2+B2
wb.save(file)
wb.close()
# ==========2.调用WPS COM后台重算并保存缓存==========
recalc_excel_by_wps_com(file)
# ==========3. openpyxl读取计算后的值==========
wb_final = load_workbook(file, data_only=True)
val = wb_final.active["C2"].value
print(f"C2计算结果 = {val}")
wb_final.close()