I'm looking for a python module to convert a .xlsx excel file to .pdf. My excel file will have formatted cells and embedded images. Does any one have any recommendations?
Asked
Active
Viewed 5,185 times
3 Answers
2
I would suggest you to use Aspose.Cells Cloud SDK to convert XLSX to PDF. Have a look on short example:
#set file name
filename = "input.xls"
outputfilename = "output.pdf"
#upload file to aspose cloud storage
storageApi.PutCreate(Path=filename)
#convert file to pdf
response = cellsApi.PostDocumentSaveAs(name=filename, body=body, newfilename=outputfilename)
destfilename = response.SaveResult.DestDocument.Href
#download converted file from storage server
response = storageApi.GetDownload(Path=destfilename)
Hope it helps. Otherwise, feel free to ask me.
Note: I am working as Developer Evangelist at Aspose.
2
I used the imagemagick module. I originally downloaded it to convert jpg the pdf. Just on a whim I tried it.
os.system('sudo convert file_name.xlsx file_name.pdf')
It will kick a few errors in the shell, and creates a folder for it name "_dername", not in the directory I specified, and give it a weird name that starts with "magick-", followed by some random characters. But it does work. In my code, I then use the os module again to move it a rename it.
os.system('sudo mv /home/pi/_dername/magick-*.pdf new_name.pdf')
This is how I got it going. Feels like the long way to do it, but hey, it work! :)
CowardlyDoomsday
- 53
- 1
- 8
0
This may be helpful. Try this code..........
from openpyxl import load_workbook
from PDFWriter import PDFWriter
workbook = load_workbook('fruits2.xlsx', guess_types=True, data_only=True)
worksheet = workbook.active
pw = PDFWriter('fruits2.pdf')
pw.setFont('Courier', 12)
pw.setHeader('XLSXtoPDF.py - convert XLSX data to PDF')
pw.setFooter('Generated using openpyxl and xtopdf')
ws_range = worksheet.iter_rows('A1:H13')
for row in ws_range:
s = ''
for cell in row:
if cell.value is None:
s += ' ' * 11
else:
s += str(cell.value).rjust(10) + ' '
pw.writeLine(s)
pw.savePage()
pw.close()
Ganesh
- 31
- 7