pdf转word源码

pdf转word

 

 

 

# -*- coding: utf-8 -*-
# @Time    : 2023/4/14 16:09
# @File    : gpdf2word.py
# @Software: PyCharm Community Edition

import os
import tkinter as tk
from tkinter import filedialog
import PyPDF2
import docx

def convert_pdf_to_word():
    # 打开文件选择对话框
    root = tk.Tk()
    root.withdraw()
    file_path = filedialog.askopenfilename(title="选择要转换的 PDF 文件", filetypes=[("PDF Files", "*.pdf")])

    # 读取 PDF 文件内容
    pdf_file = open(file_path, 'rb')
    pdf_reader = PyPDF2.PdfFileReader(pdf_file)
    text = ''
    for i in range(pdf_reader.numPages):
        text += pdf_reader.getPage(i).extractText()

    # 创建 Word 文档并保存
    doc_file = docx.Document()
    doc_file.add_paragraph(text)
    new_file_path = os.path.splitext(file_path)[0] + '.docx'
    doc_file.save(new_file_path)

    # 提示转换成功
    tk.messagebox.showinfo("提示", "PDF 转换为 Word 成功!")


# 创建 GUI 界面
root = tk.Tk()
root.title("PDF 转 Word")
root.geometry("400x200")

# 添加按钮
button = tk.Button(root, text="选择 PDF 文件", command=convert_pdf_to_word)
button.pack(pady=20)

root.mainloop()

 

 
posted @ 2023-04-14 18:14  aiplus  阅读(397)  评论(0)    收藏  举报
悬浮按钮示例