0

如何使用 Python3 和 win32com.client 显示/显示 Excel 工作簿中的所有公式,就像我可以使用 Cntrl-`

我相信我必须使用Windows DisplayFormulas 属性,但我不知道如何访问 ActiveWindow 以在 Python 中执行此操作。

这是我打开电子表格和第一个工作簿并保存它的代码:

import win32com.client

excel = win32com.client.Dispatch("Excel.Application")
wb = excel.Workbooks.Open(wb_path)
ws_index_list = [1] 
wb.WorkSheets(ws_index_list).Select()

// would like to toggle to show the formulas here before saving

wb.ActiveSheet.ExportAsFixedFormat(0, pdf_path)
wb.Close(False)

从键盘我只需输入 Cntrl-`,我想在处理大约 30 个 Excel 电子表格时自动执行此操作。

4

1 回答 1

0

您首先需要访问应用程序的Window对象Excel。然后您可以调用该DisplayFormulas方法:

import win32com.client

excel = win32com.client.Dispatch("Excel.Application")
wb = excel.Workbooks.Open(wb_path)
ws_index_list = [1] 
wb.WorkSheets(ws_index_list).Select()

#toggle DisplayFormulas
for w in excel.Windows:
   w.DisplayFormulas = True

wb.ActiveSheet.ExportAsFixedFormat(0, pdf_path)
wb.Close(False)
于 2018-11-03T15:13:47.560 回答