Convert the image in .zip to PDF with Python

Overview

--This is the code to convert the images in the .zip in the same directory to PDF. --A4 PDF (variable)

code

First, move the current directory (cd) to the location of the .py file

zip2pdf.py


import os

os.chdir(os.path.dirname(os.path.abspath(__file__)))

Get all zips in cd Here we use the glob module

zip2pdf.py


import glob

zips_ = glob.glob("./*.zip")

Get the file name (excluding extension) for each .zip Create a decompression directory under cd from the file name

zip2pdf.py


for zip_path_ in zips_:
    filename_ = os.path.split(zip_path_)[1].split(".")[0]
    unzip_dir_ = os.path.join(".", filename_)

Now that you have a decompression directory, unzip the zip Unzip the zip using the zipfile module as follows

import zipfile
zip_ = zipfile.ZipFile(zip_path_)
zip_.extractall(unzip_dir_)
zip_.close()

List only the unzipped zip files with the image extension (.png / .jpg / .jpeg)

    image_list_ = []
    for item_ in os.listdir(unzip_dir_):
        image_path_ = os.path.join(unzip_dir_, item_)
        if os.path.isfile(image_path_):
            fileext = os.path.splitext(image_path_)[-1].lower()
            if fileext in [".jpg ", ".jpeg ", ".png "]:
                image_list_.append(image_path_)
            else:
                continue
        else:
            continue

Create a pdf directory under cd and convert to pdf The simplest way to create a directory is to set exist_ok = True with the option of os.makedirs.

import img2pdf
os.makedirs("./pdf", exist_ok=True)
pdf_path_ = os.path.join(".", "pdf", "{}.pdf".format(filename_))

layout = img2pdf.get_layout_fun(params_.pagesize_)
with open(pdf_path_, 'wb') as f:
    f.write(img2pdf.convert(image_list_, layout_fun=layout))

Delete the directory used for decompression Delete the contents of the directory with rmtree of the shutil module

import shutil
shutil.rmtree(unzip_dir_)

Complete system

zip2pdf.py


import os
import glob
import zipfile
import img2pdf
import shutil

#cd Where this file is located
os.chdir(os.path.dirname(os.path.abspath(__file__)))

class params_:
    pagesize_ = (img2pdf.mm_to_pt(210), img2pdf.mm_to_pt(297))#A4size

def zip_to_pdf():
    #Get all cd zip
    zips_ = glob.glob("./*.zip")

    for zip_path_ in zips_:
        print(os.path.split(zip_path_)[1])

        #Get only the name of the zip
        filename_ = os.path.split(zip_path_)[1].split(".")[0]

        unzip_dir_ = os.path.join(".", filename_)

        #Create dir with the same name as zip on cd → unzip
        zip_ = zipfile.ZipFile(zip_path_)
        zip_.extractall(unzip_dir_)
        zip_.close()

        image_list_ = []
        #List the contents of the unzipped files(Image only)
        for item_ in os.listdir(unzip_dir_):
            image_path_ = os.path.join(unzip_dir_, item_)
            if os.path.isfile(image_path_):
                fileext = os.path.splitext(image_path_)[-1].lower()
                if fileext in [".jpg ", ".jpeg ", ".png "]:
                    image_list_.append(image_path_)
                else:
                    continue
            else:
                continue

        if len(image_list_) == 0:
            print("no image files")
            continue

        image_list_.sort()
        
        os.makedirs("./pdf", exist_ok=True)
        pdf_path_ = os.path.join(".", "pdf", "{}.pdf".format(filename_))

        layout = img2pdf.get_layout_fun(params_.pagesize_)
        with open(pdf_path_, 'wb') as f:
            f.write(img2pdf.convert(image_list_, layout_fun=layout))

        #Unzip destination dir deleted
        shutil.rmtree(unzip_dir_)

if __name__ == "__main__":
    zip_to_pdf()

Reference link

For the PDF conversion part, I referred to Collecting image files into PDF with Python.

Recommended Posts

Convert the image in .zip to PDF with Python
Convert markdown to PDF in Python
Convert PDF to image with ImageMagick
Sample to convert image to Wavelet with Python
Convert files written in python etc. to pdf with syntax highlighting
[Automation] Extract the table in PDF with Python
Tweet with image in Python
How to convert / restore a string with [] in python
[Python] Get the numbers in the graph image with OCR
Python OpenCV tried to display the image in text.
Workflow to convert formula (image) to python
In the python command python points to python3.8
Convert list to DataFrame with python
I tried to find the entropy of the image with python
Probably the easiest way to create a pdf with Python3
[Note] How to write QR code and description in the same image with python
Determine the date and time format in Python and convert to Unixtime
Try logging in to qiita with Python
Convert memo at once with Python 2to3
HTML email with image to send with python
Convert from PDF to CSV with pdfplumber
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Convert psd file to png in Python
Display Python 3 in the browser with MAMP
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
How to work with BigQuery in Python
I tried to process the image in "sketch style" with OpenCV
Convert FX 1-minute data to 5-minute data with Python
Convert PDF files to PNG files with GIMP
How to adjust image contrast in Python
I tried to process the image in "pencil style" with OpenCV
Easy image processing in Python with Pillow
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
To work with timestamp stations in Python
Convert from Markdown to HTML in Python
Convert absolute URLs to relative URLs in Python
The road to compiling to Python 3 with Thrift
How to crop the lower right part of the image with Python OpenCV
How to get the date and time difference in seconds with python
Try to image the elevation data of the Geographical Survey Institute with Python
I want to convert a table converted to PDF in Python back to CSV
I want to batch convert the result of "string" .split () in Python
Get and convert the current time in the system local timezone with python
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
Move the turtle to the place where you click the mouse with turtle in Python
I tried "smoothing" the image with Python + OpenCV
How to use the C library in Python
Log in to the remote server with SSH
Library comparison summary to generate PDF with Python
Load the network modeled with Rhinoceros in Python ③
Convert FBX files to ASCII <-> BINARY in Python
Crop the image to rounded corners with pythonista
How to use Python Image Library in python3 series
The easiest way to synthesize speech with python
What is wheezy in the Docker Python image?
Try to solve the man-machine chart with Python
Specify the Python executable to use with virtualenv
Convert svg file to png / ico with Python
How to crop an image with Python + OpenCV