Print chosen worksheets in excel files to pdf in python
This video explains
Print chosen worksheets in excel files to pdf in python
--
Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3
--
Chapters
00:00 Question
02:12 Accepted answer (Score 34)
03:33 Thank you
--
Full question
https://stackoverflow.com/questions/1668...
Question links:
[How can I open an Excel file in Python?]: https://stackoverflow.com/questions/3239...
[http://www.python-excel.org/]: http://www.python-excel.org/
[https://secure.simplistix.co.uk/svn/xlrd...]: https://secure.simplistix.co.uk/svn/xlrd...?
[http://dag.wieers.com/home-made/unoconv/]: http://dag.wieers.com/home-made/unoconv/
[https://gist.github.com/mprihoda/2891437]: https://gist.github.com/mprihoda/2891437
Accepted answer links:
[https://github.com/spottedzebra/excel/bl...]: https://github.com/spottedzebra/excel/bl...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #excel #pdf #printing #export
#avk47
Print chosen worksheets in excel files to pdf in python
--
Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3
--
Chapters
00:00 Question
02:12 Accepted answer (Score 34)
03:33 Thank you
--
Full question
https://stackoverflow.com/questions/1668...
Question links:
[How can I open an Excel file in Python?]: https://stackoverflow.com/questions/3239...
[http://www.python-excel.org/]: http://www.python-excel.org/
[https://secure.simplistix.co.uk/svn/xlrd...]: https://secure.simplistix.co.uk/svn/xlrd...?
[http://dag.wieers.com/home-made/unoconv/]: http://dag.wieers.com/home-made/unoconv/
[https://gist.github.com/mprihoda/2891437]: https://gist.github.com/mprihoda/2891437
Accepted answer links:
[https://github.com/spottedzebra/excel/bl...]: https://github.com/spottedzebra/excel/bl...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #excel #pdf #printing #export
#avk47
ACCEPTED ANSWER
Score 37
This seems like the place to put this answer.
In the simplest form:
import win32com.client
o = win32com.client.Dispatch("Excel.Application")
o.Visible = False
wb_path = r'c:\user\desktop\sample.xls'
wb = o.Workbooks.Open(wb_path)
ws_index_list = [1,4,5] #say you want to print these sheets
path_to_pdf = r'C:\user\desktop\sample.pdf'
wb.WorkSheets(ws_index_list).Select()
wb.ActiveSheet.ExportAsFixedFormat(0, path_to_pdf)
Including a little formatting magic that scales to fit to a single page and sets the print area:
import win32com.client
o = win32com.client.Dispatch("Excel.Application")
o.Visible = False
wb_path = r'c:\user\desktop\sample.xls'
wb = o.Workbooks.Open(wb_path)
ws_index_list = [1,4,5] #say you want to print these sheets
path_to_pdf = r'C:\user\desktop\sample.pdf'
print_area = 'A1:G50'
for index in ws_index_list:
#off-by-one so the user can start numbering the worksheets at 1
ws = wb.Worksheets[index - 1]
ws.PageSetup.Zoom = False
ws.PageSetup.FitToPagesTall = 1
ws.PageSetup.FitToPagesWide = 1
ws.PageSetup.PrintArea = print_area
wb.WorkSheets(ws_index_list).Select()
wb.ActiveSheet.ExportAsFixedFormat(0, path_to_pdf)
I have also started a module over a github if you want to look at that: https://github.com/spottedzebra/excel/blob/master/excel_to_pdf.py
ANSWER 2
Score 0
Yes Kashyap, I concluded that in python, it is not possible basically. SO I went into the realm of JVM languages instead.