sam*_*sam 3 python pdf excel automation
我有一个主文件夹,其中包含一些 .xlsx、.ipynb、.jpeg 和一些子文件夹。现在我想将主文件夹中的所有 .xlsx 文件转换为 PDF。
这是我每天必须做的日常工作,如果你教我如何用 python 来做,我将不胜感激。
*所有文件在工作簿的第一张纸中都有一些数据
谢谢
小智 7
有什么你已经尝试过的吗?
我建议测试 pywin32。
python3 -m pip install pywin32
Run Code Online (Sandbox Code Playgroud)
import win32com.client
from pywintypes import com_error
# Path to original excel file
WB_PATH = r'~/path/to/file.xlsx'
# PDF path when saving
PATH_TO_PDF = r'~/path/to/file.pdf'
excel = win32com.client.Dispatch("Excel.Application")
excel.Visible = False
try:
print('Start conversion to PDF')
# Open
wb = excel.Workbooks.Open(WB_PATH)
# Specify the sheet you want to save by index. 1 is the first (leftmost) sheet.
ws_index_list = [1,2,3,4,5,6,7,8,9,10,11,12]
wb.WorkSheets(ws_index_list).Select()
# Save
wb.ActiveSheet.ExportAsFixedFormat(0, PATH_TO_PDF)
except com_error as e:
print('failed.')
else:
print('Succeeded.')
finally:
wb.Close()
excel.Quit()
Run Code Online (Sandbox Code Playgroud)