3
2

More than 3 years have passed since last update.

Pythonで.zipの中の画像をPDFにする

Last updated at Posted at 2020-08-01

概要

  • 同じディレクトリ内の.zipを対象に、その中の画像をPDF化するコードです
  • A4のPDFにします(可変)

コード

まずは.pyファイルの場所にカレントディレクトリ(cd)を移動

zip2pdf.py
import os

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

cd内のzipを全て取得
ここではglobモジュールを使用します

zip2pdf.py
import glob

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

個別の.zipに対してそのファイル名(拡張子除く)を取得
ファイル名からcd下に解凍用ディレクトリを作成

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

解凍用ディレクトリが用意できたので、zipを解凍します
zip解凍はzipfileモジュールを使用して以下のように行います

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

解凍したzipファイルの内、画像拡張子(.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

cd下にpdfディレクトリを作成しpdf化
ディレクトリ作成はos.makedirsのオプションでexist_ok=Trueするのが一番シンプルですね

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

解凍に使用したディレクトリを削除
shutilモジュールのrmtreeでディレクトリの中身まで削除します

import shutil
shutil.rmtree(unzip_dir_)

完成系

zip2pdf.py
import os
import glob
import zipfile
import img2pdf
import shutil

# cd このファイルが置かれている場所
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():
    # cdのzip全取得
    zips_ = glob.glob("./*.zip")

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

        # zipの名前のみ取得
        filename_ = os.path.split(zip_path_)[1].split(".")[0]

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

        #cdにzipと同じ名前のdir作成→解凍
        zip_ = zipfile.ZipFile(zip_path_)
        zip_.extractall(unzip_dir_)
        zip_.close()

        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

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

        # 解凍先dir削除
        shutil.rmtree(unzip_dir_)

if __name__ == "__main__":
    zip_to_pdf()

参考リンク

PDF化の部分はPythonで画像ファイルをPDFにまとめるを参考にさせていただきました。

3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2