自动刷新 Excel 电子表格的Python脚本
在本文中,我们将学习如何使用Python自动刷新 Excel 电子表格。
因此,要打开 Excel 应用程序和工作簿,我们将使用 pywin32 模块。您可以使用以下代码安装模块:
pip install pywin32
然后我们将使用 win32com.client.Dispatch() 方法打开 Excel 应用程序,并使用 Workbooks.open() 方法打开工作簿。
Syntax: File.Workbooks.open(PATH_OF_FILE)
Parameters: It will take the path of the excel file as its parameter.
然后使用 RefershAll() 刷新文件:
Workbook.RefreshAll()
在本例中,我们创建了一个名为“Book1”的 Excel 文件,内容如下:
下面是实现:
Python3
# Importing the pywin32 module
import win32com.client
# Opening Excel software using the win32com
File = win32com.client.Dispatch("Excel.Application")
# Optional line to show the Excel software
File.Visible = 1
# Opening your workbook
Workbook = File.Workbooks.open("Book1.xlsx")
# Refeshing all the shests
Workbook.RefreshAll()
# Saving the Workbook
Workbook.Save()
# Closing the Excel File
File.Quit()
输出: