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 in Others by Kithuzzz
• 20,660 points
179 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 by narikkadan
• 37,660 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 in Others by narikkadan
• 37,660 points
190 views
0 votes
1 answer
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 in Others by narikkadan
• 37,660 points
61 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 in Others by narikkadan
• 37,660 points
57 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 in Others by narikkadan
• 37,660 points
164 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 in Others by narikkadan
• 37,660 points
60 views
0 votes
1 answer

Python Excel Writer (xlswriter) Insert Image from URL

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

answered Nov 17 in Others by narikkadan
• 37,660 points
40 views
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 in Others by narikkadan
• 37,660 points
111 views
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