【发布时间】:2019-02-08 08:26:03
【问题描述】:
我知道有关于这个问题的答案,但请听我说完。
我目前正在尝试在 python 中使用 img2pdf 将 .jpg 文件制作成 PDF,但不是将文件附加到 PDF,而是覆盖 PDF 中已经存在的页面。
这是代码
import os,img2pdf
os.chdir("/home/aditya/Desktop")#images are inside desktop
root, dir, files = list(os.walk(os.getcwd()))[0]#files contains the
list of all names of all .jpg file
which I want to convert into PDF
with open("pdf_file.pdf","ab") as f:#PDF file is set to append
for img_file in files:
with open(img_file,"rb") as im_file:#read bytes from the image files
f.write(img2pdf.convert(im_file))#this line overwrites the exisiting
pages in the pdf despite the fact that
I have set it to #append
有什么理由吗?我需要传递什么特殊属性吗?
感谢任何帮助。谢谢
【问题讨论】:
标签: python-3.x image pdf