0
如何使用fpdf將目錄中的所有圖像輸出到相同的pdf輸出。我可以將文件夾中的最後一個文件輸出爲pdf,或者爲每個圖像輸出多個pdf,但不是所有圖像都以同一pdf的形式顯示,如同一個目錄。我確定它是for循環,但不能正確分類。fpdf帶有多個圖像的單輸出
from fpdf import FPDF
from PIL import Image
import glob
import os
# set here
image_directory = '/home/User/Desktop/images/'
extensions = ('*.jpg','*.jpeg','*.png','*.gif')
# set 0 if you want to fit pdf to image
# unit : pt
margin = 10
imagelist=[]
for ext in extensions:
imagelist.extend(glob.glob(os.path.join(image_directory,ext)))
pdf = FPDF(unit="pt", format=[width + 2*margin, height + 2*margin])
pdf.add_page()
cover = Image.open(imagePath)
width, height = cover.size
for imagePath in imagelist:
pdf.image(imagePath, margin, margin)
destination = os.path.splitext(imagePath)[0]
pdf.output(destination + ".pdf", "F")
作品太棒了!感謝你的分享! – Xonshiz