python3 实现pdf转word工具(附:源码及成品下载)

背景

pdf转word是办公中很常见的一个操作,大多遇到这个情况都是想要再pdf文档的基础上做调整与修改,很多人在网上找各种工具来尝试,但是转换的效果一般,而且大多都是收费的,于是就用pthon3 搞了一版pdf转word工具

实现界面

pdf转word工具

代码

代码总共两个文件:main.ui、main.py

main.ui界面采用pygubu-designer进行设计,代码如下:

<?xml version='1.0' encoding='utf-8'?>
<interface version="1.2">
  <object class="tk.Toplevel" id="toplevel1">
    <property name="height">400</property>
    <property name="title" translatable="yes">PDF工具-- By 何三笔记(466867714)</property>
    <property name="width">400</property>
    <child>
      <object class="ttk.Frame" id="frame4">
        <property name="height">200</property>
        <property name="width">200</property>
        <layout manager="pack">
          <property name="expand">true</property>
          <property name="fill">both</property>
          <property name="side">top</property>
        </layout>
        <child>
          <object class="ttk.Label" id="label3">
            <property name="borderwidth">3</property>
            <property name="cursor">hand2</property>
            <property name="font">{Microsoft YaHei UI Light} 14 {bold underline}</property>
            <property name="text" translatable="yes">何三笔记</property>
            <bind sequence="&lt;1&gt;" handler="open_h3blog" add="" />
            <layout manager="pack">
              <property name="pady">10</property>
              <property name="side">top</property>
            </layout>
          </object>
        </child>
      </object>
    </child>
    <child>
      <object class="ttk.Frame" id="frame2">
        <property name="height">200</property>
        <property name="width">200</property>
        <layout manager="pack">
          <property name="expand">true</property>
          <property name="fill">both</property>
          <property name="side">top</property>
        </layout>
        <child>
          <object class="ttk.Notebook" id="notebook1">
            <property name="height">200</property>
            <property name="width">500</property>
            <layout manager="pack">
              <property name="expand">true</property>
              <property name="fill">both</property>
              <property name="side">top</property>
            </layout>
            <child>
              <object class="ttk.Notebook.Tab" id="tab1">
                <property name="compound">left</property>
                <property name="text" translatable="yes">PDF转word</property>
                <child>
                  <object class="ttk.Frame" id="frame3">
                    <property name="height">200</property>
                    <property name="width">500</property>
                    <layout manager="pack">
                      <property name="expand">true</property>
                      <property name="fill">both</property>
                      <property name="side">top</property>
                    </layout>
                    <child>
                      <object class="ttk.Frame" id="frame5">
                        <property name="height">200</property>
                        <property name="width">200</property>
                        <layout manager="grid">
                          <property name="column">0</property>
                          <property name="padx">20</property>
                          <property name="pady">20</property>
                          <property name="row">0</property>
                        </layout>
                        <child>
                          <object class="ttk.Label" id="label2">
                            <property name="text" translatable="yes">PDF文件路径:</property>
                            <layout manager="grid">
                              <property name="column">0</property>
                              <property name="padx">5</property>
                              <property name="pady">10</property>
                              <property name="row">0</property>
                            </layout>
                          </object>
                        </child>
                        <child>
                          <object class="ttk.Entry" id="entry1">
                            <property name="textvariable">string:pdf_path_var</property>
                            <property name="width">30</property>
                            <layout manager="grid">
                              <property name="column">1</property>
                              <property name="padx">5</property>
                              <property name="pady">20</property>
                              <property name="row">0</property>
                            </layout>
                          </object>
                        </child>
                        <child>
                          <object class="ttk.Button" id="button1">
                            <property name="command" type="command" cbtype="simple">select_pdf_path</property>
                            <property name="text" translatable="yes">选择</property>
                            <layout manager="grid">
                              <property name="column">2</property>
                              <property name="padx">5</property>
                              <property name="row">0</property>
                            </layout>
                          </object>
                        </child>
                        <child>
                          <object class="ttk.Label" id="label4">
                            <property name="text" translatable="yes">word导出路径:</property>
                            <layout manager="grid">
                              <property name="column">0</property>
                              <property name="row">1</property>
                            </layout>
                          </object>
                        </child>
                        <child>
                          <object class="ttk.Entry" id="entry3">
                            <property name="textvariable">string:word_path_var</property>
                            <property name="width">30</property>
                            <layout manager="grid">
                              <property name="column">1</property>
                              <property name="row">1</property>
                            </layout>
                          </object>
                        </child>
                        <child>
                          <object class="ttk.Button" id="button3">
                            <property name="command" type="command" cbtype="simple">word_save_path</property>
                            <property name="text" translatable="yes">选择</property>
                            <layout manager="grid">
                              <property name="column">2</property>
                              <property name="row">1</property>
                            </layout>
                          </object>
                        </child>
                        <child>
                          <object class="ttk.Button" id="button4">
                            <property name="command" type="command" cbtype="simple">pdf_to_word</property>
                            <property name="text" translatable="yes">pdf 转 word</property>
                            <layout manager="grid">
                              <property name="column">1</property>
                              <property name="pady">10</property>
                              <property name="row">2</property>
                            </layout>
                          </object>
                        </child>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

main.py功能实现代码:

#!/usr/bin/python3
import pathlib
import tkinter.ttk as ttk
import pygubu
from tkinter import filedialog,messagebox
from pathlib import Path
from pdf2docx import Converter
import webbrowser

PROJECT_PATH = pathlib.Path(__file__).parent
PROJECT_UI = PROJECT_PATH / "main.ui"


class MainApp:
    def __init__(self, master=None):
        self.builder = builder = pygubu.Builder()
        builder.add_resource_path(PROJECT_PATH)
        builder.add_from_file(PROJECT_UI)
        self.mainwindow = builder.get_object("toplevel1", master)

        self.pdf_path_var = None
        self.word_path_var = None
        builder.import_variables(self, ["pdf_path_var", "word_path_var"])

        builder.connect_callbacks(self)

    def run(self):
        self.mainwindow.mainloop()

    def select_pdf_path(self):
        pickedfiletypes = (('txt files', '*.pdf'), ('All', '*'))
        file_path = filedialog.askopenfilename(title='选择PDF文件',filetypes= pickedfiletypes)
        if file_path == '':
            return
        self.pdf_path_var.set(file_path)
        if self.word_path_var.get().strip() == '':
            self.word_path_var.set(str(Path(file_path).parent))
        pass

    def word_save_path(self):
        path = filedialog.askdirectory(title='选择word保存目录')
        if path == '':
            return
        self.word_path_var.set(path)
        pass

    def pdf_to_word(self):
        pdf_path = self.pdf_path_var.get()
        name = str(Path(pdf_path).name).replace('.pdf','.docx').replace('.PDF', '.docx')
        docx_path = Path(self.word_path_var.get()) / name
        cv = Converter(self.pdf_path_var.get())
        cv.convert(docx_path, start=0, end=None, multi_processing=True)
        cv.close()
        messagebox.showinfo(title='提示', message='转换完成')
        pass

    def open_h3blog(self, event=None):
        webbrowser.open('http://www.h3blog.com')


if __name__ == "__main__":
    app = MainApp()
    app.run()

采用pyinstaller打包

pyinstaller -F --hidden-import=pygubu --add-data="main.ui;." .\main.py

成品下载

何三笔记 关注公众号何三笔记,回复"pdf转word工具" 就可以下载了