phi*_*l_t 7 python excel win32com
我正在尝试将 Excel 文件转换为 PDF。在 python和Python 中使用Print selected worksheets in excel files to pdf - Converting XLSX to PDF,我编写了下面的代码。
这可以毫无问题地将 Excel 转换为 PDF,但会打开 Excel 文件。我认为这样做的目的.Visible = False是为了防止这种情况发生?我希望 excel 对象保持隐藏状态,因为我正在对 100 多个文件执行此操作,并且我不希望 excel 打开 100 次。
import win32com.client
import os
import re
nm = 'Sample.xlsx'
excel = win32com.client.Dispatch("Excel.Application")
excel.Visible = False
wb = excel.Workbooks.Open('{0}\\{1}'.format(os.getcwd(), nm))
wb.WorkSheets('Report').Select()
nm_pdf = re.sub('.xlsx', '.pdf', nm, count = 1)
wb.ActiveSheet.ExportAsFixedFormat(0, '{0}\\{1}'.format(os.getcwd(), nm_pdf))
#excel.Quit()
Run Code Online (Sandbox Code Playgroud)
Neither of the methods above worked for me but finally this did the job, maybe it's gonna be of some use for someone:
excel.ScreenUpdating = False
excel.DisplayAlerts = False
excel.EnableEvents = False
Run Code Online (Sandbox Code Playgroud)
*set it all back to True after you finish processing the file.
| 归档时间: |
|
| 查看次数: |
11646 次 |
| 最近记录: |