本篇文章旨在实现将图片转换为PDF,并将多个PDF合并成一个PDF。
import os
import glob
import fitz
import PyPDF2
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
def convert_pic_pdf(img_path, pdf_path):
for img in sorted(glob.glob(img_path)):
doc = fitz.open()
imgdoc = fitz.open(img)
pdfbytes = imgdoc.convertToPDF()
imgpdf = fitz.open("pdf", pdfbytes)
doc.insertPDF(imgpdf)
if not os.path.exists(pdf_path):
doc.save(pdf_path)
def get_merged_pdf(imgs_path):
save_pdf_path = os.path.join(imgs_path, 'pdf')
img_names_list = os.listdir(imgs_path)
pic_name_list = ['.jpg', '.png', '.bmp', '.jpeg', '.JPG', '.PNG', '.JPEG']
img_n_list = [name for name in img_names_list if os.path.splitext(name)[-1] in pic_name_list]
for img_n in img_n_list:
img_p = os.path.join(imgs_path, img_n)
pdf_n = os.path.splitext(img_n)[0] + '.pdf'
pdf_p = os.path.join(save_pdf_path, pdf_n)
if not os.path.exists(save_pdf_path):
os.makedirs(save_pdf_path)
convert_pic_pdf(img_p, pdf_p)
pdf_names_list = os.listdir(save_pdf_path)
pdf_writer = PyPDF2.PdfFileWriter()
for pdf_n in pdf_names_list:
pdf_p = os.path.join(save_pdf_path, pdf_n)
pdf_obj = open(pdf_p, 'rb')
pdf_reader = PyPDF2.PdfFileReader(pdf_obj)
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
combine_pdf_path = os.path.join(imgs_path, 'all_pics.pdf')
pdf_output_file = open(combine_pdf_path, 'wb')
pdf_writer.write(pdf_output_file)
pdf_output_file.close()
return combine_pdf_path
if __name__ == '__main__':
continue_re = 'y'
while continue_re != 'n':
imgs_path = input('请输入需转换为PDF的图片路径,回车结束,类似(D:/pic): ')
if not imgs_path:
imgs_path = "D:/pic"
pdf_path = get_merged_pdf(imgs_path)
print("转换结束!合并后的PDF文件为:{}".format(pdf_path))
continue_re = input('请输入是否继续转换,若继续输入 y,结束输入 n :')
print()
参考链接: (1)https://blog.csdn.net/u012034742/article/details/106295154 (2)https://zhuanlan.zhihu.com/p/36014414
|