前提准备条件: pip install PyMuPDF
import glob
import fitz
import os
def pic2pdf():
doc = fitz.open( )
for img in sorted(glob.glob("pic2pdf/*")):
assert isinstance(img, object)
print(img)
imgdoc = fitz.open(img)
pdfbytes = imgdoc.convertToPDF()
imgpdf = fitz.open("pdf", pdfbytes)
doc.insertPDF(imgpdf)
if os.path.exists("allimages.pdf"):
os.remove("allimages.pdf")
doc.save("allimages.pdf")
doc.close()
if __name__ == '__main__':
pic2pdf()