[英]For a batch of pdf files in a directory, using Python find if there's a landscape page and rotate it to portrait
[英]python Portrait and landscape page in pdf
我是 python 的新手。 我想生成一個帶有 3 張圖像的 pdf,其中一張是肖像,第二張是風景,第三張是肖像。 但似乎下面的代碼無法處理這種情況,我錯過了什么嗎?
images = []
images = glob.glob(Outpath + "/IMG/*.jpg" ,recursive=False)
pdf = FPDF()
for x in range(len(images)):
print(images[x] + ' at x = ' + str(x))
#pdf.add_page()
if width > height:
pdf.add_page(orientation='L')
pdf.image(images[x],x=0,y=0,h=210,w=297)
elif width < height:
pdf.add_page(orientation='P')
pdf.image(images[x],x=0,y=0,h=297,w=210)
pdf.output(Outpath + "/IMG/IO.pdf", "F")
您將在每次迭代中創建一個新的 object。
一次創建一個變量並在以后的迭代中使用它,如下所示:
import glob
from fpdf import FPDF
images = []
images = glob.glob(Outpath + "/IMG/*.jpg" ,recursive=False)
pdf = FPDF()
for x in range(len(images)):
im_int = Image.open(images[x])
width = im_int.width
height = im_int.height
if width > height:
pdf.add_page(orientation='L')
pdf.image(images[x],x=0,y=0,h=210,w=297)
else:
pdf.add_page()
pdf.image(images[x],x=0,y=0,h=297,w=210)
pdf.output(Outpath + "/IMG/IO.pdf", "F")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.