import argparse from PIL import Image import os from PyPDF2 import PdfFileReader, PdfFileWriter def img_to_pdf(img_path, save_path): img_list = os.listdir(img_path) out_pdf = PdfFileWriter() sdfs = [os.path.join(img_path, i) for i in img_list] for f in sdfs: img = Image.open(f) fw = f.replace('.jpg', '.pdf') img.save(fw) out_pdf.appendPagesFromReader(PdfFileReader( open(fw, 'rb'))) # 也可拆这句为 sc_pdf=PdfFileReader(open(fw,'rb')); out_pdf.addPage(sc_pdf.getPage(0)) out_pdf.write(open(save_path, 'wb')) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('--img_path', type=str, default='./images') parser.add_argument('--save_path', type=str, default='./pdf_file') args = parser.parse_args() img_to_pdf(args.img_path, args.save_path)