Convert Excel file to PDF with Python and output.
Install Win32com package (for Excel operation through COM in Python) https://github.com/mhammond/pywin32/releases From here, download and install according to your environment (python version, 64-bit or 32-bit)
① Import the sample_pdf.xlsx file ② Create PDF file name (pdf_ [title] _ [createdate] .pdf * [] is obtained from Excel) ③ Output a PDF file
pdf_create.py
import pathlib
import openpyxl
from win32com import client
#Excel file to read
in_filepass = pathlib.Path("./data/sample_pdf.xlsx")
#win32com > client >Get Excel operation object
xlApp = client.Dispatch("Excel.Application")
#sample_pdf.Get xlsx (* resolve is relative path ⇒ absolute path)
book = xlApp.workbooks.open(str(in_filepass.resolve()))
for sheet in book.Worksheets:
#Extract title (cell C2) from Excel
title = str(sheet.Range("C2").value)
#Extract the material creation date (cell H2) from Excel
createdate = str(int(sheet.Range("H2").value))
#Create PDF file path obj to output (pdf)_[title]_[createdate].pdf)
out_filepass = pathlib.Path("./data/output/pdf_" + title + "_" + createdate + ".pdf")
#Output PDF file (* resolve is relative path ⇒ absolute path)
sheet.ExportAsFixedFormat(0, str(out_filepass.resolve()))
book.Close()
xlApp.Quit()
PDF is also available. I feel like I've done it! However, PDF seems to be able to be returned to Excel when searching the net, so if you are in trouble if it is tampered with, it seems that you can not use it. It's a win32com package, but it seems that you can do various things. Create shortcuts and save emails automatically. I found it interesting to look into this area.
That's all, thank you (*'▽')
Recommended Posts