Converting all tabs of excel sheet to PDF

0 votes

The first tab of my excel sheet, which has two tabs, has a summary of the second tab. Both tabs need to be converted to PDF format.

I used to below code

# converting xlsx to PDF
from win32com import client
import win32api
input_file = r'D:\DL-KBRB-Jan22-2.xlsx'
#give your file name with valid path 
output_file = r'D:\DL-KBRB-Jan22-2.pdf'
#give valid output file name and path
app = client.DispatchEx("Excel.Application")
app.Interactive = False
app.Visible = False
Workbook = app.Workbooks.Open(input_file)
try:
    Workbook.ActiveSheet.ExportAsFixedFormat(0, output_file)
except Exception as e:
    print("Failed to convert in PDF format.Please confirm environment meets all the requirements  and try again")
    print(str(e))
finally:
    Workbook.Close()
    app.Exit()

The problem with the above code is, it is converting only the first tab of the excel sheet to pdf but not the second tab. Not sure how to go about the same. Also, I'm getting the below error (though the PDF is converted)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-8d02b700e303> in <module>
     17 finally:
     18     Workbook.Close()
---> 19     app.Exit()

~\anaconda3\lib\site-packages\win32com\client\dynamic.py in __getattr__(self, attr)
    525 
    526                 # no where else to look.
--> 527                 raise AttributeError("%s.%s" % (self._username_, attr))
    528 
    529         def __setattr__(self, attr, value):

AttributeError: Excel.Application.Exit
Sep 25, 2022 in Others by Kithuzzz
• 38,010 points
1,334 views

1 answer to this question.

0 votes

Using VBA, try it like this, for a used range on a sheet.

Worksheets(Array("Sheet1", "Sheet2")).Select

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Consolidated.pdf", Quality:= xlQualityStandard, IncludeDocProperties:=True, _
     IgnorePrintAreas:=False, OpenAfterPublish:=True

Or, if you want specific ranges, do it like this.

one = Worksheets("Sheet1").Range("A1:B10")
two = Worksheets("Sheet2").Range("A1:B10")
three = Worksheets("Sheet3").Range("A1:B10")

    Worksheets(Array(one, two, three)).Select
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Consolidated.pdf", Quality:= xlQualityStandard, IncludeDocProperties:=True, _
         IgnorePrintAreas:=False, OpenAfterPublish:=True

one = ""
two = ""
three = ""
answered Sep 26, 2022 by narikkadan
• 63,420 points

Related Questions In Others

0 votes
1 answer

Java Spring - Writing Excel file and converting to PDF

Since you are using Spring I suggest ...READ MORE

answered Sep 26, 2022 in Others by narikkadan
• 63,420 points
1,982 views
0 votes
1 answer

How can I convert excel to PDF by Libreoffice and keep all format from excel file?

"Times New Roman" typeface does not have ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,420 points
1,309 views
0 votes
1 answer

How to get sum of all matches of HLOOKUP in Excel?

Consider: =SUMPRODUCT((A1:E1="apple")*(A2:E2)) To include more ...READ MORE

answered Oct 16, 2022 in Others by narikkadan
• 63,420 points
2,129 views
0 votes
1 answer

Conversion of PDF file to Excel in R

I looked at the pdf, and it ...READ MORE

answered Oct 16, 2022 in Others by narikkadan
• 63,420 points
1,322 views
0 votes
1 answer

How search data in excel with openpyxl?

The method iter_rows in the library has changed, refer ...READ MORE

answered Oct 2, 2022 in Others by narikkadan
• 63,420 points
5,761 views
0 votes
1 answer

How to insert info into online excel spreadsheet with python selenium

I recently discovered one of my options, ...READ MORE

answered Oct 24, 2022 in Others by narikkadan
• 63,420 points
935 views
0 votes
1 answer

Python Excel Writer (xlswriter) Insert Image from URL

Use this: url = "..../picture.jpg" data = urllib.request.urlopen(url).read() file = ...READ MORE

answered Nov 17, 2022 in Others by narikkadan
• 63,420 points
1,464 views
0 votes
1 answer
0 votes
1 answer

How to print an Excel Sheet using VBA with Nitro PDF Creator in Excel 2016

you can use the built-in excel facilities ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,420 points
885 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP