👋
Pythonで、Zipのwebp画像を一括でpdfに変換するスクリプト
動作環境
Python:3.10.6 64-bit
GoogleChrome:127.0.6533.120(Official Build)(64 ビット)
OS:Windows10
CPU:Core i5-1135G7
メモリ:16GB
スクリプト
import os
import zipfile
from PIL import Image
import img2pdf
def extract_and_convert_to_jpg(zip_file_path, output_folder):
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
folder_name = os.path.splitext(os.path.basename(zip_file_path))[0]
extraction_path = os.path.join(output_folder, folder_name)
zip_ref.extractall(extraction_path)
for root, _, files in os.walk(extraction_path):
for file in files:
if file.lower().endswith('.webp'):
webp_path = os.path.join(root, file)
jpg_path = os.path.splitext(webp_path)[0] + '.jpg'
# Convert .webp to .jpg
img = Image.open(webp_path)
img = img.convert('RGB') # RGBAからRGBに変換
img.save(jpg_path, 'JPEG')
# Remove the original .webp file
os.remove(webp_path)
# Convert jpg files in the output folder to a single PDF
pdf_output_folder = "C:/hogehoge"
os.makedirs(pdf_output_folder, exist_ok=True)
pdf_output_path = os.path.join(pdf_output_folder, folder_name + '.pdf')
image_list = [os.path.join(extraction_path, f) for f in os.listdir(extraction_path) if f.lower().endswith('.jpg')]
image_list.sort()
with open(pdf_output_path, "wb") as f:
f.write(img2pdf.convert(image_list))
print("Conversion complete. PDF created at:", pdf_output_path)
# フォルダ内の全ての.zipファイルを処理する場合
folder_path = "C:/hogehoge"
for root, _, files in os.walk(folder_path):
for file in files:
if file.lower().endswith('.zip'):
zip_file_path = os.path.join(root, file)
extract_and_convert_to_jpg(zip_file_path, root)
Discussion