I tried to find out how to streamline the work flow with Excel x Python ⑤

Purpose of this time

Convert Excel file to PDF with Python and output.

Advance preparation

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)

Program flow

① Import the sample_pdf.xlsx file ② Create PDF file name (pdf_ [title] _ [createdate] .pdf * [] is obtained from Excel) ③ Output a PDF file

Program execution result confirmation

Excel to import (sample_pdf.xlsx)

キャプチャ.JPG

Output file (pdf_ [title] _ [createdate] .pdf)

キャプチャ.JPG キャプチャ.JPG

program

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()

Summary

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

I tried to find out how to streamline the work flow with Excel x Python ②
I tried to find out how to streamline the work flow with Excel x Python ④
I tried to find out how to streamline the work flow with Excel x Python ⑤
I tried to find out how to streamline the work flow with Excel x Python ①
I tried to find out how to streamline the work flow with Excel x Python ③
I tried to find out how to streamline the work flow with Excel × Python, my article summary ★
I tried to find the entropy of the image with python
I tried to simulate how the infection spreads with Python
I tried to find out if ReDoS is possible with Python
I tried to improve the efficiency of daily work with Python
Excel X Python The fastest way to work
I tried to streamline the standard role of new employees with Python
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to solve the problem with Python Vol.1
I tried to find out the outline about Big Gorilla
I tried to find the average of the sequence with TensorFlow
I tried to put out the frequent word ranking of LINE talk with Python
I tried to divide the file into folders with Python
python beginners tried to find out
The 15th offline real-time I tried to solve the problem of how to write with python
I tried to solve the ant book beginner's edition with python
How to work with BigQuery in Python
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
A story that didn't work when I tried to log in with the Python requests module
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
How to write offline real time I tried to solve E11 with python
Mayungo's Python Learning Episode 2: I tried to put out characters with variables
I tried to get the authentication code of Qiita API with Python.
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
I tried to get the movie information of TMDb API with Python
How to write offline real time I tried to solve E12 with python
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
I tried to save the data with discord
[Python] How to read excel file with pandas
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried "binarizing" the image with Python + OpenCV
[Algorithm x Python] How to use the list
I tried to automate sushi making with python
I tried to open the latest data of the Excel file managed by date in the folder with Python
I tried to output the rpm list of SSH login destination to an Excel sheet with Python + openpyxl.
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
I tried to find out as much as possible about the GIL that you should know if you are doing parallel processing with Python
When I tried to create a virtual environment with Python, it didn't work
I tried to easily visualize the tweets of JAWS DAYS 2017 with Python + ELK
I tried to automatically send the literature of the new coronavirus to LINE with Python
[Python & SQLite] I tried to analyze the expected value of a race with horses in the 1x win range ①
I tried to explain how to get the article content with MediaWiki API in an easy-to-understand manner with examples (Python 3)
I tried to learn the sin function with chainer
I tried to summarize how to use matplotlib of python
[Introduction to Python] How to iterate with the range function?
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I tried to draw a route map with Python
[Python] How to specify the download location with youtube-dl
I tried to get started with blender python script_Part 02
I tried to implement an artificial perceptron with python
I want to inherit to the back with python dataclass