前言
开发这个软件还得从最早是21年说起。当时正值6月毕业答辩,学校让上传毕业资料。其中包括论文,开题报告,以及中期报告,外文翻译等,要求资料整合到一个word中。当时整合文档时光修改格式就难倒了一大批同学,很多学生的office还不是特别熟悉,修改格式更是难上加难。所以大家就想了一个 简单的方法,将这些文档的PDF文件拼接到一起就好,只用修改页码。PDF拼接合并不会修改文本格式的。这个想法虽然是好的,但网上很多PDF拼接软件都是收费的。所以当时有Python基础的我就想到的自动化办公或许可以解决这个问题,查阅了资料后,发现果然可以用Python实现。所以当时用了大概4小时时间就写好了PDF拼接合并软件,包括UI设计等。但是当时毕业季挺忙的,就没发布出来,只是同学间使用。 现在22年了,都毕业一年了。刚入职比亚迪,新员工的电脑还没审批下来,所以没什么工作安排,就在办公室熟悉项目,所以就利用工作的空闲时间将这个软件开发完整。
一、软件具备什么功能?
PDF拆分 PDF合并 PDF转Word PDF加密 PDF解密 Word转PDF功能纯属胡扯,WPS免费就搞定了。
二、开发步骤
2.1.引入库
代码如下(示例):
import os,ast
import choose,cut,merge,pdfcvword,wordcvpdf,encypdf,decr
from PyQt5 import QtWidgets
from PyPDF2 import PdfFileReader,PdfFileWriter
import tkinter as tk
from tkinter import filedialog
from pdf2docx import Converter
2.2.PDF拆分功能设计
该功能是搭配UI界面去设计的,所以必要时要配合UI界面去看。
拆分功能代码如下:
def choose_cut_filepath():
root = tk.Tk()
root.withdraw()
f_path=filedialog.askopenfilename()
cut_ui.lineEdit_2.setText(f_path)
def read_page():
try:
path = cut_ui.lineEdit_2.text()
pdf_reader = PdfFileReader(path)
num = pdf_reader.getNumPages()
cut_ui.lineEdit_3.setText(str(num))
except Exception as e:
cut_ui.lineEdit_3.setText(str(e))
def select_cut_outputh():
global cut_path
root = tk.Tk()
root.withdraw()
f_path = filedialog.askdirectory()
cut_ui.lineEdit.setText(f_path)
cut_path=f_path
def cut_pdf():
try:
global cut_path
num1=cut_ui.lineEdit_4.text()
num1=ast.literal_eval(num1)
path1=os.path.join(cut_path,'拆分1.pdf')
path2 = os.path.join(cut_path,'拆分2.pdf')
path=cut_ui.lineEdit_2.text()
pdf_reader = PdfFileReader(path)
pdf_writer1 = PdfFileWriter()
pdf_writer2 = PdfFileWriter()
num2 = pdf_reader.getNumPages()
if num1<num2:
for page1 in range(0,num1):
pdf_writer1.addPage(pdf_reader.getPage(page1))
with open(path1,'wb') as out:
pdf_writer1.write(out)
for page2 in range(num1, num2):
pdf_writer2.addPage(pdf_reader.getPage(page2))
with open(path2, 'wb') as out:
pdf_writer2.write(out)
cut_ui.lineEdit_5.setText(cut_path)
else:
cut_ui.lineEdit_5.setText('拆分页码不符合要求')
except Exception as e:
cut_ui.lineEdit_5.setText(str(e))
主程序里的UI调用代码如下
cutwindow= QtWidgets.QWidget()
cut_ui=cut.Ui_Form()
cut_ui.setupUi(cutwindow)
cut_ui.pushButton.clicked.connect(choose_cut_filepath)
cut_ui.pushButton_2.clicked.connect(read_page)
cut_ui.pushButton_3.clicked.connect(cut_pdf)
cut_ui.pushButton_4.clicked.connect(back_choosewindow)
cut_ui.pushButton_5.clicked.connect(select_cut_outputh)
2.3 PDF合并
UI界面如下
合并功能代码如下
def choose_merge_filepath():
global file_path
root = tk.Tk()
root.withdraw()
f_path = filedialog.askdirectory()
merge_ui.lineEdit_1.setText(f_path)
file_path=f_path
def get_filename():
global file_path
for paths,dirs,files in os.walk(file_path):
text = str(files)
merge_ui.textEdit.setText(text)
def merge_pdf():
try:
global file_path
list1=[]
files=merge_ui.textEdit.toPlainText()
files=ast.literal_eval(files)
output = os.path.join(file_path,'拼接.pdf')
for name in files:
merge_path=os.path.join(file_path,name)
list1.append(merge_path)
pdf_writer = PdfFileWriter()
for path in list1:
pdf_reader =PdfFileReader(path)
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
with open(output,'wb') as out:
pdf_writer.write(out)
merge_ui.lineEdit_2.setText(str(output))
except Exception as e:
merge_ui.lineEdit_2.setText(str(e))
主程序UI界面代码如下
mergewindow = QtWidgets.QWidget()
merge_ui = merge.Ui_Form()
merge_ui.setupUi(mergewindow)
merge_ui.pushButton.clicked.connect(choose_merge_filepath)
merge_ui.pushButton_2.clicked.connect(get_filename)
merge_ui.pushButton_3.clicked.connect(merge_pdf)
merge_ui.pushButton_4.clicked.connect(back_choosewindow)
2.4PDF转Word
UI界面如下所示
功能代码如下
def choose_pdfcvfile():
root = tk.Tk()
root.withdraw()
f_path = filedialog.askopenfilename()
pdf_reader = PdfFileReader(f_path)
num = pdf_reader.getNumPages()
pdfcvwd_ui.lineEdit.setText(str(f_path))
pdfcvwd_ui.lineEdit_2.setText((str(num)))
def zdpdfcv():
try:
path = pdfcvwd_ui.lineEdit.text()
root = tk.Tk()
root.withdraw()
f_path = filedialog.askdirectory()
f_path=os.path.join(f_path,'部分转换.docx')
s=pdfcvwd_ui.lineEdit_3.text()
e=pdfcvwd_ui.lineEdit_4.text()
print(s)
print(type(s))
print(e)
doc=Converter(path)
doc.convert(f_path,s,e)
doc.close()
pdfcvwd_ui.lineEdit_5.setText(f_path)
except Exception as e:
pdfcvwd_ui.lineEdit_5.setText(str(e))
def allpdfcv():
try:
path = pdfcvwd_ui.lineEdit.text()
f_path = filedialog.askdirectory()
f_path = os.path.join(f_path, '全部转换.docx')
doc = Converter(path)
doc.convert(f_path, start=0, end=None)
doc.close()
pdfcvwd_ui.lineEdit_6.setText(f_path)
except Exception as e:
pdfcvwd_ui.lineEdit_6.setText(str(e))
主程序UI代码运行如下
pdfcvwdwindow = QtWidgets.QWidget()
pdfcvwd_ui=pdfcvword.Ui_Form()
pdfcvwd_ui.setupUi(pdfcvwdwindow)
pdfcvwd_ui.pushButton.clicked.connect(choose_pdfcvfile)
pdfcvwd_ui.pushButton_2.clicked.connect(zdpdfcv)
pdfcvwd_ui.pushButton_3.clicked.connect(allpdfcv)
pdfcvwd_ui.pushButton_4.clicked.connect(back_choosewindow)
2.5 解密和加密
UI界面如下
功能代码如下
def choose_decrpdf():
root = tk.Tk()
root.withdraw()
f_path = filedialog.askopenfilename()
decrpdf_ui.lineEdit.setText(f_path)
def ency():
try:
f_path = encypdf_ui.lineEdit.text()
pwd = encypdf_ui.lineEdit_2.text()
pdf_reader = PdfFileReader(f_path)
pdf_writer = PdfFileWriter()
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
pdf_writer.encrypt(pwd)
with open(f_path,'wb') as out:
pdf_writer.write(out)
encypdf_ui.lineEdit_3.setText('文件加密成功')
except Exception as e:
encypdf_ui.lineEdit_3.setText(str(e))
def choose_enpdf():
root = tk.Tk()
root.withdraw()
f_path = filedialog.askopenfilename()
encypdf_ui.lineEdit.setText(f_path)
def decrpdf():
try:
f_path = decrpdf_ui.lineEdit.text()
pwd = decrpdf_ui.lineEdit_2.text()
pdf_reader = PdfFileReader(f_path)
pdf_reader.decrypt(pwd)
pdf_writer = PdfFileWriter()
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
with open(f_path,'wb') as out:
pdf_writer.write(out)
decrpdf_ui.lineEdit_3.setText('文件解密成功')
except Exception as e:
decrpdf_ui.lineEdit_3.setText(str(e))
主程序UI代码如下
encywindow = QtWidgets.QWidget()
encypdf_ui = encypdf.Ui_Form()
encypdf_ui.setupUi(encywindow)
encypdf_ui.pushButton.clicked.connect(choose_enpdf)
encypdf_ui.pushButton_2.clicked.connect(ency)
encypdf_ui.pushButton_3.clicked.connect(back_choosewindow)
decrwindow = QtWidgets.QWidget()
decrpdf_ui = decr.Ui_Form()
decrpdf_ui.setupUi(decrwindow)
decrpdf_ui.pushButton.clicked.connect(choose_decrpdf)
decrpdf_ui.pushButton_2.clicked.connect(decrpdf)
decrpdf_ui.pushButton_3.clicked.connect(back_choosewindow)
三 程序打包
程序打包的话我之前的博客也发过,用pyinstaller就可以了
四 总结+软件+主程序
打包好的软件我已经上传至CSDN了,再留一个百度网盘链接吧 百度网盘: 网盘入口 提取码:1234 下方给出主程序代码。没有给出U界面的代码。需要完整工程代码的可以私信。
附完整主程序代码,UI界面的py代码太多了就不一一附了
import os,ast
import choose,cut,merge,pdfcvword,wordcvpdf,encypdf,decr
from PyQt5 import QtWidgets
from PyPDF2 import PdfFileReader,PdfFileWriter
import tkinter as tk
from tkinter import filedialog
from pdf2docx import Converter
def choose_merge_filepath():
global file_path
root = tk.Tk()
root.withdraw()
f_path = filedialog.askdirectory()
merge_ui.lineEdit_1.setText(f_path)
file_path=f_path
def get_filename():
global file_path
for paths,dirs,files in os.walk(file_path):
text = str(files)
merge_ui.textEdit.setText(text)
def merge_pdf():
try:
global file_path
list1=[]
files=merge_ui.textEdit.toPlainText()
files=ast.literal_eval(files)
output = os.path.join(file_path,'拼接.pdf')
for name in files:
merge_path=os.path.join(file_path,name)
list1.append(merge_path)
pdf_writer = PdfFileWriter()
for path in list1:
pdf_reader =PdfFileReader(path)
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
with open(output,'wb') as out:
pdf_writer.write(out)
merge_ui.lineEdit_2.setText(str(output))
except Exception as e:
merge_ui.lineEdit_2.setText(str(e))
def choose_cut_filepath():
root = tk.Tk()
root.withdraw()
f_path=filedialog.askopenfilename()
cut_ui.lineEdit_2.setText(f_path)
def read_page():
try:
path = cut_ui.lineEdit_2.text()
pdf_reader = PdfFileReader(path)
num = pdf_reader.getNumPages()
cut_ui.lineEdit_3.setText(str(num))
except Exception as e:
cut_ui.lineEdit_3.setText(str(e))
def select_cut_outputh():
global cut_path
root = tk.Tk()
root.withdraw()
f_path = filedialog.askdirectory()
cut_ui.lineEdit.setText(f_path)
cut_path=f_path
def cut_pdf():
try:
global cut_path
num1=cut_ui.lineEdit_4.text()
num1=ast.literal_eval(num1)
path1=os.path.join(cut_path,'拆分1.pdf')
path2 = os.path.join(cut_path,'拆分2.pdf')
path=cut_ui.lineEdit_2.text()
pdf_reader = PdfFileReader(path)
pdf_writer1 = PdfFileWriter()
pdf_writer2 = PdfFileWriter()
num2 = pdf_reader.getNumPages()
if num1<num2:
for page1 in range(0,num1):
pdf_writer1.addPage(pdf_reader.getPage(page1))
with open(path1,'wb') as out:
pdf_writer1.write(out)
for page2 in range(num1, num2):
pdf_writer2.addPage(pdf_reader.getPage(page2))
with open(path2, 'wb') as out:
pdf_writer2.write(out)
cut_ui.lineEdit_5.setText(cut_path)
else:
cut_ui.lineEdit_5.setText('拆分页码不符合要求')
except Exception as e:
cut_ui.lineEdit_5.setText(str(e))
def choose_pdfcvfile():
root = tk.Tk()
root.withdraw()
f_path = filedialog.askopenfilename()
pdf_reader = PdfFileReader(f_path)
num = pdf_reader.getNumPages()
pdfcvwd_ui.lineEdit.setText(str(f_path))
pdfcvwd_ui.lineEdit_2.setText((str(num)))
def zdpdfcv():
try:
path = pdfcvwd_ui.lineEdit.text()
root = tk.Tk()
root.withdraw()
f_path = filedialog.askdirectory()
f_path=os.path.join(f_path,'部分转换.docx')
s=pdfcvwd_ui.lineEdit_3.text()
e=pdfcvwd_ui.lineEdit_4.text()
print(s)
print(type(s))
print(e)
doc=Converter(path)
doc.convert(f_path,s,e)
doc.close()
pdfcvwd_ui.lineEdit_5.setText(f_path)
except Exception as e:
pdfcvwd_ui.lineEdit_5.setText(str(e))
def allpdfcv():
try:
path = pdfcvwd_ui.lineEdit.text()
f_path = filedialog.askdirectory()
f_path = os.path.join(f_path, '全部转换.docx')
doc = Converter(path)
doc.convert(f_path, start=0, end=None)
doc.close()
pdfcvwd_ui.lineEdit_6.setText(f_path)
except Exception as e:
pdfcvwd_ui.lineEdit_6.setText(str(e))
def choose_decrpdf():
root = tk.Tk()
root.withdraw()
f_path = filedialog.askopenfilename()
decrpdf_ui.lineEdit.setText(f_path)
def ency():
try:
f_path = encypdf_ui.lineEdit.text()
pwd = encypdf_ui.lineEdit_2.text()
pdf_reader = PdfFileReader(f_path)
pdf_writer = PdfFileWriter()
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
pdf_writer.encrypt(pwd)
with open(f_path,'wb') as out:
pdf_writer.write(out)
encypdf_ui.lineEdit_3.setText('文件加密成功')
except Exception as e:
encypdf_ui.lineEdit_3.setText(str(e))
def choose_enpdf():
root = tk.Tk()
root.withdraw()
f_path = filedialog.askopenfilename()
encypdf_ui.lineEdit.setText(f_path)
def decrpdf():
try:
f_path = decrpdf_ui.lineEdit.text()
pwd = decrpdf_ui.lineEdit_2.text()
pdf_reader = PdfFileReader(f_path)
pdf_reader.decrypt(pwd)
pdf_writer = PdfFileWriter()
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
with open(f_path,'wb') as out:
pdf_writer.write(out)
decrpdf_ui.lineEdit_3.setText('文件解密成功')
except Exception as e:
decrpdf_ui.lineEdit_3.setText(str(e))
def choose_wdcvfile():
root = tk.Tk()
root.withdraw()
f_path = filedialog.askopenfilename()
wdcvpdf_ui.lineEdit.setText(str(f_path))
def zdwdcv():
try:
pass
except Exception as e:
wdcvpdf_ui.lineEdit_5.setText(str(e))
def allwdcv():
try:
pass
except Exception as e:
wdcvpdf_ui.lineEdit_6.setText(str(e))
def open_cutwindow():
cutwindow.show()
choosewindow.close()
def open_mergewindow():
mergewindow.show()
choosewindow.close()
def open_pdfcvword():
choosewindow.close()
pdfcvwdwindow.show()
def open_encryption_pdf():
choosewindow.close()
encywindow.show()
def open_decipher_pdf():
choosewindow.close()
decrwindow.show()
def open_wordcvpdf():
wdcvpdfwindow.show()
choosewindow.close()
def back_choosewindow():
choosewindow.show()
cutwindow.close()
mergewindow.close()
pdfcvwdwindow.close()
wdcvpdfwindow.close()
encywindow.close()
decrwindow.close()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
choosewindow= QtWidgets.QWidget()
choose_ui =choose.Ui_Form()
choose_ui.setupUi(choosewindow)
choose_ui.pushButton.clicked.connect(open_cutwindow)
choose_ui.pushButton_2.clicked.connect(open_mergewindow)
choose_ui.pushButton_3.clicked.connect(open_pdfcvword)
choose_ui.pushButton_4.clicked.connect(open_encryption_pdf)
choose_ui.pushButton_5.clicked.connect(open_decipher_pdf)
choose_ui.pushButton_6.clicked.connect(open_wordcvpdf)
choosewindow.show()
cutwindow= QtWidgets.QWidget()
cut_ui=cut.Ui_Form()
cut_ui.setupUi(cutwindow)
cut_ui.pushButton.clicked.connect(choose_cut_filepath)
cut_ui.pushButton_2.clicked.connect(read_page)
cut_ui.pushButton_3.clicked.connect(cut_pdf)
cut_ui.pushButton_4.clicked.connect(back_choosewindow)
cut_ui.pushButton_5.clicked.connect(select_cut_outputh)
mergewindow = QtWidgets.QWidget()
merge_ui = merge.Ui_Form()
merge_ui.setupUi(mergewindow)
merge_ui.pushButton.clicked.connect(choose_merge_filepath)
merge_ui.pushButton_2.clicked.connect(get_filename)
merge_ui.pushButton_3.clicked.connect(merge_pdf)
merge_ui.pushButton_4.clicked.connect(back_choosewindow)
pdfcvwdwindow = QtWidgets.QWidget()
pdfcvwd_ui=pdfcvword.Ui_Form()
pdfcvwd_ui.setupUi(pdfcvwdwindow)
pdfcvwd_ui.pushButton.clicked.connect(choose_pdfcvfile)
pdfcvwd_ui.pushButton_2.clicked.connect(zdpdfcv)
pdfcvwd_ui.pushButton_3.clicked.connect(allpdfcv)
pdfcvwd_ui.pushButton_4.clicked.connect(back_choosewindow)
encywindow = QtWidgets.QWidget()
encypdf_ui = encypdf.Ui_Form()
encypdf_ui.setupUi(encywindow)
encypdf_ui.pushButton.clicked.connect(choose_enpdf)
encypdf_ui.pushButton_2.clicked.connect(ency)
encypdf_ui.pushButton_3.clicked.connect(back_choosewindow)
decrwindow = QtWidgets.QWidget()
decrpdf_ui = decr.Ui_Form()
decrpdf_ui.setupUi(decrwindow)
decrpdf_ui.pushButton.clicked.connect(choose_decrpdf)
decrpdf_ui.pushButton_2.clicked.connect(decrpdf)
decrpdf_ui.pushButton_3.clicked.connect(back_choosewindow)
app.exec_()
人生,只有尝试过的人,才有资格说不后悔。 很庆幸,前半生没有遗憾 很幸运,后半生有所期待
|