![](/img/trans.png)
[英]How can I find jpg. files from a list for a given folder and copy them to another folder location?
[英]How can I convert all JPG files in a folder to PDFs and combine them?
我的文件夾(/ img /)中有一些1924×2972 JPG文件。 我想要1)將它們轉換為PDF,以及2)將它們合並為一個PDF。 但是我沒有成功。 我是python的新手。 請讓我知道如何進行。
from PIL import Image
import glob
from fpdf import FPDF #
imagelist = []
for filename in glob.glob('img/*.jpg'): #assuming jpg
im=Image.open(filename)
imagelist.append(im)
pdf = FPDF()
# imagelist is the list with all image filenames
for image in imagelist:
pdf.add_page()
pdf.image(image, 10,210,297)
pdf.output("yourfile.pdf", "F")
錯誤:
File "<ipython-input-39-9fb5b6258b5e>", line 1, in <module>
pdf.image(image, 10,210,297)
File "/Users/xyz/anaconda/lib/python3.6/site-packages/fpdf/fpdf.py", line 150, in wrapper
return fn(self, *args, **kwargs)
File "/Users/xyz/anaconda/lib/python3.6/site-packages/fpdf/fpdf.py", line 960, in image
if not name in self.images:
TypeError: unhashable type: 'JpegImageFile'
在第二個循環中,您需要實際引用每個jpg的路徑:
pdf.image(image, 10,210,297)
編輯:另外,我認為您只需要每個圖像的路徑(而不是使用Image.open
打開每個圖像。請嘗試以下操作:
import glob
from fpdf import FPDF #
imagelist = glob.glob('img/*.jpg')
pdf = FPDF()
# imagelist is the list with all image filenames
for image in imagelist:
pdf.add_page()
pdf.image(image, 10,210,297)
pdf.output("yourfile.pdf", "F")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.