“显示公式"在excel中使用python& win32com [英] "show formulas" in excel using python & win32com

查看:273
本文介绍了“显示公式"在excel中使用python& win32com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can display/show all of the formulas in an Excel workbook using Python3 and win32com.client, like I can do interactively with Cntrl-`

我相信我必须使用 Windows DisplayFormulas属性,但是我不知道如何在Python中访问ActiveWindow来完成此操作.

I believe I have to use the Windows DisplayFormulas Property but I don't know how to access the ActiveWindow to do this in Python.

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

Here is the code I have up to open a spreadsheet and the first workbook and save it:

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电子表格,所以我想使它自动化.

From the keyboard I'd simply enter Cntrl-`, I'd like to automate this as I'm processing about 30 excel spreadsheets.

推荐答案

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

You will first need to access the Window object of the Excel application. You can then call the DisplayFormulas method:

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)

这篇关于“显示公式"在excel中使用python& win32com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆