JPG2PDF
2020-08-27 本文已影响0人
Noza_ea8f
import PySimpleGUI as sg
from lib.func import jpg2pdf
layout = [[sg.Text('选择"JPG图片"文件夹')], [sg.Input(), sg.FolderBrowse('浏览')],
[sg.Text('选择"PDF"输出"文件夹')], [sg.Input(), sg.FolderBrowse('浏览')],
[sg.Button('开始处理'), sg.Button(' 关闭 ')]]
window = sg.Window('JPG2PDF', layout, icon='icon/bomb64.ico')
while True:
event, values = window.read()
path_jpg = str(values[0]) + '/'
path_pdf = str(values[1]) + '/'
# print(path_jpg)
# print(path_pdf)
if event in (None, ' 关闭 '):
break
try:
jpg2pdf(img_path=path_jpg, pdf_path=path_pdf)
sg.Popup('程序执行完毕!', no_titlebar=True)
except Exception as e:
sg.Popup('请检查选择的文件路径是否正确!', no_titlebar=True)
window.close()
# pyinstaller -F -w jpg2pdf.py --noconsole
func.py
import glob
import fitz # 导入本模块需安装pymupdf库
import os
def jpg2pdf(img_path, pdf_path):
'''
将文件夹中所有jpg图片分别转换为同一名称的pdf文件,并保存至指定文件夹
:param img_path:
:param pdf_path:
:return:
'''
for img in glob.glob(img_path + "\*.jpg"):
file_name = os.path.basename(img).replace('jpg', 'pdf')
doc = fitz.open()
imgdoc = fitz.open(img)
pdfbytes = imgdoc.convertToPDF()
imgpdf = fitz.open("pdf", pdfbytes)
doc.insertPDF(imgpdf)
doc.save(pdf_path + '\\' + file_name)
doc.close()
img_path = 'jpg'
pdf_path = 'pdf'
jpg2pdf(img_path, pdf_path)