--This is the code to convert the images in the .zip in the same directory to PDF. --A4 PDF (variable)
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_)
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()
For the PDF conversion part, I referred to Collecting image files into PDF with Python.
Recommended Posts