Python用img2pdf库批量转换图片为PDF文件
import os
import time
import img2pdf
localtime = time.localtime()
timesign = time.strftime("%Y%m%d%H%M%S", localtime)
print(time.strftime("%Y%m%d%H%M%S", localtime))
imgpath = 'imgs'
try:
with open('PDF' + timesign + '.pdf', 'wb+') as f:
imgs =[]
for fname in os.listdir(imgpath):
if not fname.endswith('.jpg'):
continue
path = os.path.join(imgpath, fname)
if os.path.isdir(path):
continue
imgs.append(path)
f.write(img2pdf.convert(imgs))
print("文件保存至outout.pdf")
except OSError as err:
print("OS error: {0}".format(err))