0

This code below helps to print worksheets (1,2,4) into 3 separate PDF file.

The issue is, it take sheet as order (1,2,4) instead of sheet name.

If I want to print 3 sheets with name: Mysheet1, yoursheet, hersheet, how can I print them to pdf?

Can you please help?

Thanks HR

import win32com.client

o = win32com.client.Dispatch("Excel.Application")

o.Visible = False

# Path to Excel file
wb_path = r'C:\tempfile/Sample Invoice.xlsx'
# Open this Excel file
wb = o.Workbooks.Open(wb_path)

# list the sheet you want to print, first sheet is 1, use sheet order, not actual sheet name
ws_index_list = [1,2,4]

for i in ws_index_list:
    path_to_pdf = fr'C:\tempfile/sample{i}.pdf'
    wb.WorkSheets(i).Select()
    wb.ActiveSheet.ExportAsFixedFormat(0, path_to_pdf)
Hary2
  • 51
  • 4
  • So you want to use actual sheet names as file names? – PM 77-1 Jul 09 '21 at 21:23
  • Does this answer your question? [How can I iterate over worksheets in win32com?](https://stackoverflow.com/questions/41407824/how-can-i-iterate-over-worksheets-in-win32com) – rfkortekaas Jul 09 '21 at 21:23

0 Answers0