0

I use code from this thread Print chosen worksheets in excel files to pdf in python to convert Excel file to Pdf.

It was working fine and suddendly, it now give me error below at this line wb.WorkSheets(ws_index_list).Select()

(The code opens Excel file fine)

AttributeError: '<win32com.gen_py.Microsoft Excel 16.0 Object Library._Workbook instance at 0x2758034291872>' object has no attribute 'WorkSheets'

---> 21 wb.WorkSheets(ws_index_list).Select() 22 wb.ActiveSheet.ExportAsFixedFormat(0, path_to_pdf)

import win32com.client
o = win32com.client.Dispatch("Excel.Application")
# o = win32.gencache.EnsureDispatch('Excel.Application')
o.Visible = False

# Path to Excel file
wb_path = r'~\Sample Invoice1.xlsx'

wb = o.Workbooks.Open(wb_path)
print('type of wb:',type(wb))
 
    
ws_index_list = [1] #say you want to print these sheets

path_to_pdf = r'~/Sample Invoice1.pdf'
wb.WorkSheets(ws_index_list).Select()
wb.ActiveSheet.ExportAsFixedFormat(0, path_to_pdf) 
0

1 Answer 1

2

Looks like the attribute is called Sheets and not WorkSheets.

So you'll change your second-to-last-line to:

wb.Sheets(ws_index_list.Select()
Sign up to request clarification or add additional context in comments.

1 Comment

Also, you'll want to make sure you quit everything when you are done. That is, you probably want to add wb.Close() and o.Quit() to the end of it all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.