Python - Converting XLSX to PDF

DavidRguez picture DavidRguez · Sep 14, 2018 · Viewed 17.1k times · Source

I have always used win32com module in my development server to easily convert from xlsx to pdf:

o = win32com.client.Dispatch("Excel.Application")
o.Visible = False
o.DisplayAlerts = False
wb = o.Workbooks.Open("test.xlsx")))
wb.WorkSheets("sheet1").Select()
wb.ActiveSheet.ExportAsFixedFormat(0, "test.pdf")
o.Quit()

However, I have deployed my Django app in production server where I don't have Excel application installed and it raises the following error:

File "C:\virtualenvs\structuraldb\lib\site-packages\win32com\client\__init__.p
y", line 95, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,c
lsctx)
  File "C:\virtualenvs\structuraldb\lib\site-packages\win32com\client\dynamic.py
", line 114, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "C:\virtualenvs\structuraldb\lib\site-packages\win32com\client\dynamic.py
", line 91, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.II
D_IDispatch)
com_error: (-2147221005, 'Invalid class string', None, None)

Is there any good alternative to convert from xlsx to PDF in Python?

I have tested xtopdf with PDFWriter, but with this solution you need to read and iterate the range and write lines one by one. I wonder if there is a more direct solution similar to win32com.client.

Thanks!

Answer

Thomas Devoogdt picture Thomas Devoogdt · May 4, 2020

As my original answer was deleted and is eventually a bit useful, I repost it here.

You could do it in 3 steps:

  1. excel to pandas: pandas.read_excel
  2. pandas to HTML: pandas.DataFrame.to_html
  3. HTML to pdf: python-pdfkit (git), python-pdfkit (pypi.org)
import pandas as pd
import pdfkit

df = pd.read_excel("file.xlsx")
df.to_html("file.html")
pdfkit.from_file("file.html", "file.pdf")

install:

sudo pip3.6 install pandas xlrd pdfkit
sudo apt-get install wkhtmltopdf