代码拉取完成,页面将自动刷新
import webbrowser
from pdf2docx import Converter
from PyPDF2 import PdfWriter, PdfReader
from cajtopdf import caj_info, caj_to_pdf
from tkinter import Tk, END, Frame, Label, Button, Text, messagebox, filedialog, Entry
def caj_to_pdf_():
caj_files = []
tip.pack(side='top')
def new():
[caj_files.remove(i) for i in caj_files if not i]
(Buttons1[0].config(state='normal'), Buttons1[1].config(state='normal')) if len(caj_files) > 0 else \
(Buttons1[0].config(state='disabled'), Buttons1[1].config(state='disabled'))
tip.delete(1.0, END)
[tip.insert('end', str(caj_info(i)) + '\n') for i in caj_files]
def select_file():
caj_files.clear()
caj_files.append(filedialog.askopenfilename(title='请选择一个CAJ文件!', filetypes=[('CAJ Files', '*.caj')]))
new()
def merge():
save_folder = filedialog.asksaveasfilename(title='请选择转换后PDF保存路径和文件名!',
filetypes=[('PDF Files', '*.pdf')])
save_folder = save_folder if save_folder.endswith('.pdf') else save_folder + '.pdf'
tip.insert('end', '预计耗时较长,且可能程序未响应,请勿关闭程序,请耐心等待!\n')
try:
caj_to_pdf(caj_files[0], save_folder)
tip.insert('end', f'\n转换后PDF文件保存为:{save_folder}\n')
messagebox.showinfo(title='转换成功!', message=f'保存为{save_folder}')
except:
tip.insert('end', '\n失败了!\n')
[button.config(text='') for button in Buttons0 + Buttons1]
[button.pack_forget() for button in Buttons0 + Buttons1 if not button.cget('text')]
Buttons0[0].config(text='选择CAJ文件', command=select_file)
Buttons1[1].config(text='转为PDF文件', command=merge)
[button.pack(side='left') for button in Buttons0 + Buttons1 if button.cget('text')]
[button.pack_forget() for button in Buttons0 + Buttons1 if not button.cget('text')]
Label1_1.pack_forget()
Entry1_1.pack_forget()
new()
def pdf_to_word_():
pdf_files = []
tip.pack(side='top')
def new():
[pdf_files.remove(i) for i in pdf_files if not i]
(Buttons1[0].config(state='normal'), Buttons1[1].config(state='normal')) if len(pdf_files) > 0 else \
(Buttons1[0].config(state='disabled'), Buttons1[1].config(state='disabled'))
tip.delete(1.0, END)
if pdf_files:
with open(pdf_files[0], 'rb') as f:
pdf = PdfReader(f)
information = pdf.metadata
txt = f'{pdf_files[0]}\n<PDF文件信息>\n作者 : {information.author}\n发明者 : {information.creator}\n' \
f'产生方式 : {information.producer}\n主题 : {information.subject}\n' \
f'标题 : {information.title}\n页数 : {len(pdf.pages)}\n '
tip.delete(1.0, END)
tip.insert('end', txt)
def select_file():
pdf_files.clear()
pdf_files.append(filedialog.askopenfilename(title='请选择一个PDF文件!', filetypes=[('PDF Files', '*.pdf')]))
new()
def merge():
tip.insert('end', '预计耗时较长,且可能程序未响应,请勿关闭程序,请耐心等待!\n')
save_folder = filedialog.asksaveasfilename(title='请选择转换后Word保存路径和文件名!',
filetypes=[('Word Files', '*.docx')])
save_folder = save_folder if save_folder.endswith('.docx') else save_folder + '.docx'
cv = Converter(pdf_files[0])
cv.convert(save_folder, start=0, end=None)
cv.close()
tip.insert('end', f'\n转换后Word文件保存为:{save_folder}\n')
messagebox.showinfo(title='转换成功!', message=f'保存为{save_folder}')
[button.config(text='') for button in Buttons0 + Buttons1]
[button.pack_forget() for button in Buttons0 + Buttons1 if not button.cget('text')]
Buttons0[0].config(text='选择PDF文件', command=select_file)
Buttons1[1].config(text='转为Word文件', command=merge)
[button.pack(side='left') for button in Buttons0 + Buttons1 if button.cget('text')]
[button.pack_forget() for button in Buttons0 + Buttons1 if not button.cget('text')]
Label1_1.pack_forget()
Entry1_1.pack_forget()
new()
def caj_info_():
tip.pack(side='top')
def info():
path = filedialog.askopenfilename(title='请选择一个CAJ文件!', filetypes=[('CAJ Files', '*.caj')])
if path:
txt = caj_info(path)
tip.delete(1.0, END)
tip.insert('end', txt)
[button.config(text='') for button in Buttons0 + Buttons1]
[button.pack_forget() for button in Buttons0 + Buttons1 if not button.cget('text')]
Buttons0[0].config(text='选择CAJ文件', command=info)
[button.pack(side='left') for button in Buttons0 + Buttons1 if button.cget('text')]
[button.pack_forget() for button in Buttons0 + Buttons1 if not button.cget('text')]
Label1_1.pack_forget()
Entry1_1.pack_forget()
def pdf_merge():
pdf_files = []
tip.pack(side='top')
def new():
[pdf_files.remove(i) for i in pdf_files if not i]
(Buttons1[0].config(state='normal'), Buttons1[1].config(state='normal')) if len(pdf_files) > 0 else \
(Buttons1[0].config(state='disabled'), Buttons1[1].config(state='disabled'))
tip.delete(1.0, END)
[tip.insert('end', str(index + 1) + ':' + str(i) + '\n') for index, i in enumerate(pdf_files)]
def select_file():
pdf_files.append(filedialog.askopenfilename(title='请选择一个PDF文件!', filetypes=[('PDF Files', '*.pdf')]))
new()
def select_files():
[pdf_files.append(_path) for _path in
filedialog.askopenfilenames(title='请选择多个PDF文件!', filetypes=[('PDF Files', '*.pdf')])]
new()
def clear():
pdf_files.clear()
new()
def merge():
save_folder = filedialog.asksaveasfilename(title='请选择PDF保存路径和文件名!',
filetypes=[('PDF Files', '*.pdf')])
save_folder = save_folder if save_folder.endswith('.pdf') else save_folder + '.pdf'
pdf_writer = PdfWriter()
for inFile in pdf_files:
pdf_reader = PdfReader(open(inFile, 'rb'))
for index in range(len(pdf_reader.pages)):
pdf_writer.add_page(pdf_reader.pages[index])
pdf_writer.write(open(save_folder, 'wb'))
tip.insert('end', f'\n合并文件保存为:{save_folder}\n')
messagebox.showinfo(title='合并成功!', message=f'保存为{save_folder}')
[button.config(text='') for button in Buttons0 + Buttons1]
[button.pack_forget() for button in Buttons0 + Buttons1 if not button.cget('text')]
Buttons0[0].config(text='逐个添加PDF文件', command=select_file)
Buttons0[1].config(text='批量添加PDF文件', command=select_files)
Buttons1[0].config(text='清空选择', command=clear)
Buttons1[1].config(text='合并文件', command=merge)
[button.pack(side='left') for button in Buttons0 + Buttons1 if button.cget('text')]
[button.pack_forget() for button in Buttons0 + Buttons1 if not button.cget('text')]
Label1_1.pack_forget()
Entry1_1.pack_forget()
new()
def pdf_info():
tip.pack(side='top')
def info():
path = filedialog.askopenfilename(title='请选择一个PDF文件!', filetypes=[('PDF Files', '*.pdf')])
if path:
with open(path, 'rb') as f:
pdf = PdfReader(f)
information = pdf.metadata
txt = f'{path}\n<PDF文件信息>\n作者 : {information.author}\n发明者 : {information.creator}\n' \
f'产生方式 : {information.producer}\n主题 : {information.subject}\n' \
f'标题 : {information.title}\n页数 : {len(pdf.pages)} '
tip.delete(1.0, END)
tip.insert('end', txt)
[button.config(text='') for button in Buttons0 + Buttons1]
[button.pack_forget() for button in Buttons0 + Buttons1 if not button.cget('text')]
Buttons0[0].config(text='选择PDF文件', command=info)
[button.pack(side='left') for button in Buttons0 + Buttons1 if button.cget('text')]
[button.pack_forget() for button in Buttons0 + Buttons1 if not button.cget('text')]
Label1_1.pack_forget()
Entry1_1.pack_forget()
def pdf_word():
path = []
tip.pack(side='top')
def new():
[path.remove(i) for i in path if not i]
(Buttons1[0].config(state='normal'), Buttons1[1].config(state='normal')) if len(path) > 0 else \
(Buttons1[0].config(state='disabled'), Buttons1[1].config(state='disabled'))
tip.delete(1.0, END)
[tip.insert('end', str(i) + '\n') for i in path]
def info():
path.clear()
path.append(filedialog.askopenfilename(title='请选择一个PDF文件!', filetypes=[('PDF Files', '*.pdf')]))
new()
page = 0
with open(path[0], 'rb') as pdf:
pdf = PdfReader(pdf)
page0 = pdf.pages[page]
text0 = page0.extract_text()
text0.encode('utf-8').decode('utf-8')
tip.insert('end', f'\n{text0}')
def show():
new()
page = int(Entry1_1.get())
with open(path[0], 'rb') as pdf:
pdf = PdfReader(pdf)
page0 = pdf.pages[page]
text0 = page0.extract_text()
text0.encode('utf-8').decode('utf-8')
tip.insert('end', f'\n{text0}')
def word():
new()
save_folder = filedialog.asksaveasfilename(title='请选择文字保存路径和文件名!',
filetypes=[('Txt Files', '*.txt')])
save_folder = save_folder if save_folder.endswith('.txt') else save_folder + '.txt'
page = int(Entry1_1.get())
with open(path[0], 'rb') as pdf:
pdf = PdfReader(pdf)
page0 = pdf.pages[page]
text0 = page0.extract_text()
text0.encode('utf-8').decode('utf-8')
with open(save_folder, 'wt') as f:
f.write(text0)
tip.insert('end', f'\n文件保存为:{save_folder}\n')
messagebox.showinfo(title='导出文字成功!', message=f'保存为{save_folder}')
[button.config(text='') for button in Buttons0 + Buttons1]
[button.pack_forget() for button in Buttons0 + Buttons1 if not button.cget('text')]
Buttons0[0].config(text='选择PDF文件', command=info)
Label1_1.config(text='文字所在页数')
Label1_1.pack(side='left')
Entry1_1.pack(side='left')
Buttons1[0].config(text='显示文字', command=show)
Buttons1[1].config(text='导出文字', command=word)
[button.pack(side='left') for button in Buttons0 + Buttons1 if button.cget('text')]
[button.pack_forget() for button in Buttons0 + Buttons1 if not button.cget('text')]
new()
def open_url():
webbrowser.open("https://gitee.com/wanglidong666/caj--pdf--word-conversion-tool/blob/WLD/README.md", new=0)
root = Tk()
root.title('CAJ_PDF_Word转换工具 1.0 by:wld')
# _w, _h = 500, 700
# root.geometry(f'{_w}x{_h}+{int((root.winfo_screenwidth() - _w) / 2)}+{int((root.winfo_screenheight() - _h) / 2)}')
Label(root, text='-' * 25 + '选择功能' + '-' * 25, font=("宋体", 12)).pack(side='top')
frame_0 = Frame(root)
frame_0.pack(side='top')
Button(frame_0, text='CAJ转PDF', font=("宋体", 12), fg="blue", command=caj_to_pdf_).pack(side='left')
Button(frame_0, text='CAJ文件信息', font=("宋体", 12), fg="blue", command=caj_info_).pack(side='left')
Label(root, text=' ' * 40, font=("宋体", 12)).pack(side='top')
frame_0_0 = Frame(root)
frame_0_0.pack(side='top')
Button(frame_0_0, text='PDF转Word', font=("宋体", 12), fg="blue", command=pdf_to_word_).pack(side='left')
Button(frame_0_0, text='PDF批量合并', font=("宋体", 12), fg="blue", command=pdf_merge).pack(side='left')
Button(frame_0_0, text='PDF文字提取', font=("宋体", 12), fg="blue", command=pdf_word).pack(side='left')
Button(frame_0_0, text='PDF文件信息', font=("宋体", 12), fg="blue", command=pdf_info).pack(side='left')
Label(root, text='-' * 25 + '功能操作' + '-' * 25, font=("宋体", 12)).pack(side='top')
frame_1 = Frame(root)
frame_1.pack(side='top')
frame_1_0 = Frame(frame_1)
frame_1_0.pack(side='top')
Buttons0 = [Button(frame_1_0, font=("宋体", 12), fg="blue") for _ in range(5)]
frame_1_1 = Frame(frame_1)
frame_1_1.pack(side='top')
Label1_1 = Label(frame_1_1, font=("宋体", 12))
Entry1_1 = Entry(frame_1_1, font=("宋体", 12), width=5)
Entry1_1.insert('end', '0')
Buttons1 = [Button(frame_1_1, font=("宋体", 12), fg="blue") for _ in range(5)]
Label(root, text='-' * 25 + '提示信息' + '-' * 25, font=("宋体", 12)).pack(side='top')
frame_2 = Frame(root)
frame_2.pack(side='top')
tip = Text(frame_2, font=("宋体", 12), width=40, height=10)
Label(root, text='-' * 25 + '关于软件' + '-' * 25, font=("宋体", 12)).pack(side='top')
Button(root, text='软件教程', font=("宋体", 12), fg="blue", command=open_url).pack(side='top')
root.mainloop()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。