加载现有 Excel 文件,更改所有工作表的缩放级别,并删除网格线

Load existing Excel file, change the zoom level for all sheets, and remove gridlines

我正在使用 win32com 加载现有的 Excel 文档。我目前能够遍历工作表并删除文本换行和自动调整列。

import win32com.client
excel = win32com.client.Dispatch('Excel.Application')
excel.Visible = False # I want to keep it this way


path = "C:\Users\username\Documents\DDA"
wb_new = excel.Workbooks.Open(path + '\new_file.xlsx')

# wb_new is a file with three tabs, with one value in cell A1 each... 
# ...if you want to recreate it.

active_sheets = wb_new.Sheets.Count
for i in range(0, active_sheets):
    ws = wb_new.Worksheets(i + 1)
    ws.Columns.WrapText = False
    ws.Columns.AutoFit()

接下来,我想调整缩放级别并删除所有工作表的网格线。我还没有找到一个解决方案可以在不制作 excel.Visible = True 的情况下完成此操作。我愿意使用来自 python-excel.org 的软件包,但我还没有找到任何东西。

我发现了 openpyxl.worksheet.views 子包,其中包含 SheetView class,但它似乎对现有文档没有用。

import win32com.client
excel = win32com.client.Dispatch('Excel.Application')
excel.Visible = False # I want to keep it this way


path = "C:\Users\username\Documents\DDA"
wb_new = excel.Workbooks.Open(path + '\new_file.xlsx')

# wb_new is a file with three tabs, with one value in cell A1 each... 
# ...if you want to recreate it.

active_sheets = wb_new.Sheets.Count
for i in range(0, active_sheets):
    ws = wb_new.Worksheets(i + 1)
    ws.Columns.WrapText = False
    ws.Columns.AutoFit()
    ws.Activate() # answer starts here
    excel.ActiveWindow.Zoom = 80
    excel.ActiveWindow.DisplayGridlines = False