[Topic DIscussion] 怎样在deepin上实现pdf文档转换为word文档?
Tofloor
poster avatar
pho
deepin
2023-08-20 09:05
Author

因为在linux系统的应用软件即使同一品牌也比windows系统的版本功能少一些,比如wps和福昕,Linux版都没有pdf转word的功能。我看到网上有一种方法是直接用命令可以做到,见下图。但我是没有实操经验,也看不懂图中写的方法。

截图_选择区域_20230820005929.png

Reply Favorite View the author
All Replies
deepin
2023-08-20 09:13
#1

wps要收费。

Reply View the author
pho
deepin
2023-08-20 09:41
#2

wps要收费。

我是wps会员,Linux版wps没有pdf转word功能

Reply View the author
deepin
2023-08-20 10:53
#3
pho

我是wps会员,Linux版wps没有pdf转word功能

功能残缺,双系统(Windows/deepin)使用更合适

Reply View the author
熊爷不是好惹的
deepin
2023-08-20 18:22
#4

agree

Reply View the author
130******35
deepin
2023-08-20 20:09
#5
利用python3我们可以制作一个简单带GUI界面的pdf-word转换器。这里提供一个简单的实现:
请先在shell里安装依赖包:pip3 install python-docx PySimpleGUI PyMuPDF pillow pyinstaller
 
然后编辑一个脚本文件(例如: pdftodocx.py),内容如下:
import PySimpleGUI as sg
import os
import io
import fitz
from docx import Document
from docx.shared import Inches
from PIL import Image
 
def pdf_to_word(pdf_file, word_file):
    doc = Document()
    pdf_document = fitz.open(pdf_file)
 
    for page_num in range(pdf_document.page_count):
        page = pdf_document.load_page(page_num)
        
        text = page.get_text()
 
        image_list = page.get_images(full=True)
        
        for img_index, img in enumerate(image_list):
            xref = img[0]
            base_image = pdf_document.extract_image(xref)
            image = Image.open(io.BytesIO(base_image["image"]))
            image_path = f"temp_image_{page_num}_{img_index}.png"
            image.save(image_path)
 
            doc.add_picture(image_path, width=Inches(6))
 
            os.remove(image_path)
 
        doc.add_paragraph(text)
        doc.add_page_break()
    
    doc.save(word_file)
 
def main():
    sg.theme('LightGrey1')
 
    layout = [
        [sg.Text("选择输入的PDF文件:", justification='right', size=(15, 1)), sg.InputText(key="pdf_file", size=(50, 1)), sg.FileBrowse()],
        [sg.Text("更改输出目录:", justification='right', size=(15, 1)), sg.InputText(key="output_directory", size=(50, 1)), sg.FolderBrowse()],
        [sg.Button("转换"), sg.Button("退出")]
    ]
 
    window = sg.Window("PDF转Word工具", layout)
 
    while True:
        event, values = window.read()
 
        if event == sg.WIN_CLOSED or event == "退出":
            break
        elif event == "转换":
            pdf_file = values["pdf_file"]
            output_directory = values["output_directory"] or os.path.dirname(pdf_file)
            output_file = os.path.join(output_directory, f"{os.path.splitext(os.path.basename(pdf_file))[0]}.docx")
 
            try:
                pdf_to_word(pdf_file, output_file)
                sg.popup("转换完成!", title="提示")
            except Exception as e:
                sg.popup(f"转换失败:{str(e)}", title="错误提示")
 
    window.close()
 
if __name__ == "__main__":
    main()
 
 
假设我们把该文件保存在你的桌面目录上,即~/桌面
打包脚本:
pyinstaller --onefile pdftodocx.py
等待打包成功后,会在桌面目录内看到一个dist目录。dist目录里就是我们的目标可执行文件:pdftodocx
然后在桌面上写一个pdftodocx.desktop文件,内容如下:
[Desktop Entry]
Name=pdftodocx
GenericName=PDF转word文档
Comment=PDF文档转换成docx文档
Exec=/home/user/桌面/dist/pdftodocx
Terminal=false
Type=Application
Name[zh_CN]=PDF转Word工具
 
然后,鼠标双击桌面上的pdftodocx.desktop就能运行我们的转换程序了(请替换Exec=/home/user/桌面/dist/pdftodocx里的user为你自己的用户名)。
选择输入文件(原始pdf文件),点击“转换”。就能在输入文件的同目录里生成.docx文件了。
 
 
PS: 编辑pdftodocx.desktop文件添加:Icon=/your_path/ico.png
通过配置一个图标的路径,就能让桌面上的pdftodocx.desktop拥有一个好看的图标了。
 
 
 
 
 
 
 
 
Reply View the author
兆兆嘟嘟嘟
deepin
2023-08-21 07:19
#6

这是python代码,运行需要python运行环境。

Reply View the author
pho
deepin
2023-08-21 08:03
#7
130******35
利用python3我们可以制作一个简单带GUI界面的pdf-word转换器。这里提供一个简单的实现:
请先在shell里安装依赖包:pip3 install python-docx PySimpleGUI PyMuPDF pillow pyinstaller
 
然后编辑一个脚本文件(例如: pdftodocx.py),内容如下:
import PySimpleGUI as sg
import os
import io
import fitz
from docx import Document
from docx.shared import Inches
from PIL import Image
 
def pdf_to_word(pdf_file, word_file):
    doc = Document()
    pdf_document = fitz.open(pdf_file)
 
    for page_num in range(pdf_document.page_count):
        page = pdf_document.load_page(page_num)
        
        text = page.get_text()
 
        image_list = page.get_images(full=True)
        
        for img_index, img in enumerate(image_list):
            xref = img[0]
            base_image = pdf_document.extract_image(xref)
            image = Image.open(io.BytesIO(base_image["image"]))
            image_path = f"temp_image_{page_num}_{img_index}.png"
            image.save(image_path)
 
            doc.add_picture(image_path, width=Inches(6))
 
            os.remove(image_path)
 
        doc.add_paragraph(text)
        doc.add_page_break()
    
    doc.save(word_file)
 
def main():
    sg.theme('LightGrey1')
 
    layout = [
        [sg.Text("选择输入的PDF文件:", justification='right', size=(15, 1)), sg.InputText(key="pdf_file", size=(50, 1)), sg.FileBrowse()],
        [sg.Text("更改输出目录:", justification='right', size=(15, 1)), sg.InputText(key="output_directory", size=(50, 1)), sg.FolderBrowse()],
        [sg.Button("转换"), sg.Button("退出")]
    ]
 
    window = sg.Window("PDF转Word工具", layout)
 
    while True:
        event, values = window.read()
 
        if event == sg.WIN_CLOSED or event == "退出":
            break
        elif event == "转换":
            pdf_file = values["pdf_file"]
            output_directory = values["output_directory"] or os.path.dirname(pdf_file)
            output_file = os.path.join(output_directory, f"{os.path.splitext(os.path.basename(pdf_file))[0]}.docx")
 
            try:
                pdf_to_word(pdf_file, output_file)
                sg.popup("转换完成!", title="提示")
            except Exception as e:
                sg.popup(f"转换失败:{str(e)}", title="错误提示")
 
    window.close()
 
if __name__ == "__main__":
    main()
 
 
假设我们把该文件保存在你的桌面目录上,即~/桌面
打包脚本:
pyinstaller --onefile pdftodocx.py
等待打包成功后,会在桌面目录内看到一个dist目录。dist目录里就是我们的目标可执行文件:pdftodocx
然后在桌面上写一个pdftodocx.desktop文件,内容如下:
[Desktop Entry]
Name=pdftodocx
GenericName=PDF转word文档
Comment=PDF文档转换成docx文档
Exec=/home/user/桌面/dist/pdftodocx
Terminal=false
Type=Application
Name[zh_CN]=PDF转Word工具
 
然后,鼠标双击桌面上的pdftodocx.desktop就能运行我们的转换程序了(请替换Exec=/home/user/桌面/dist/pdftodocx里的user为你自己的用户名)。
选择输入文件(原始pdf文件),点击“转换”。就能在输入文件的同目录里生成.docx文件了。
 
 
PS: 编辑pdftodocx.desktop文件添加:Icon=/your_path/ico.png
通过配置一个图标的路径,就能让桌面上的pdftodocx.desktop拥有一个好看的图标了。
 
 
 
 
 
 
 
 

感谢感谢!我要研究研究python了

Reply View the author