pdf转docx,这个库很好用

1,788 阅读3分钟

这是我参与8月更文挑战的第18天,活动详情查看:8月更文挑战

环境

  • windows 10 64位
  • anaconda with python 3.7
  • pdf2docx 0.5.2

前言

pdf文件转换成word文件是一个非常常见的操作,我相信,大部分人的免费解决方案是使用一些在线的转换服务,但是这里会有个数据泄露的问题。本文介绍一个开源免费的本地转换工具,pdf2docx

安装pdf2docx

安装方法非常简单,使用pip指令,执行

pip install pdf2docx

安装成功后,除了基础的库之外,pdf2docx还为我们提供了可执行文件pdf2docx

日常使用的话,直接使用可执行文件就能够进行pdfdocx的转换;如果需要在python代码中使用,那么,使用其提供的api也能够达到目的。

命令行的使用

通过pdf2docx --help可以查看命令行的具体帮助信息

INFO: Showing help with the command 'pdf2docx -- --help'.

NAME
    pdf2docx - Command line interface for ``pdf2docx``.

SYNOPSIS
    pdf2docx COMMAND | -

DESCRIPTION
    Command line interface for ``pdf2docx``.

COMMANDS
    COMMAND is one of the following:

     convert
       Convert pdf file to docx file.

     debug
       Convert one PDF page and plot layout information for debugging.

     gui
       Simple user interface.

     table
       Extract table content from pdf pages.

上述帮助列出了pdf2docx支持的指令,这里我们主要了解下convertgui

  • convert

    这是它的核心功能,convert本身也提供了很多的参数,可以通过pdf2docx convert --help来查看,这样的写法同样适用于其它指令,后面的我们就不再详细列出了

    (base) PS C:\Users\Administrator> pdf2docx.exe convert --help
    INFO: Showing help with the command 'pdf2docx convert -- --help'.
    
    NAME
        pdf2docx convert - Convert pdf file to docx file.
    
    SYNOPSIS
        pdf2docx convert PDF_FILE <flags>
    
    DESCRIPTION
        Convert pdf file to docx file.
    
    POSITIONAL ARGUMENTS
        PDF_FILE
            Type: str
            PDF filename to read from.
    
    FLAGS
        --docx_file=DOCX_FILE
            Type: Optional[str]
            Default: None
            docx filename to write to. Defaults to None.
        --password=PASSWORD
            Type: Optional[str]
            Default: None
            Password for encrypted pdf. Default to None if not encrypted.
        --start=START
            Type: int
            Default: 0
            First page to process. Defaults to 0.
        --end=END
            Type: Optional[int]
            Default: None
            Last page to process. Defaults to None.
        --pages=PAGES
            Type: Optional[list]
            Default: None
            Range of pages. Defaults to None.
        Additional flags are accepted.
            Configuration parameters.
    
            .. note
    
    NOTES
        You can also use flags syntax for POSITIONAL ARGUMENTS
    

    由上可知,要转换pdf里所有的页面,只需执行

    pdf2docx.exe convert test.pdf test.docx
    

    从第3页开始,直到结束

    pdf2docx.exe convert test.pdf test.docx --start=2
    

    从开始到第10页

    pdf2docx.exe convert test.pdf test.docx --end=10
    

    从第2页到第5页

    pdf2docx.exe convert test.pdf test.docx --start=1 --end=5
    

    要特别注意,这里的startend都是从0开始的

    当然,不连续的页面也是可以一次性转换,比如

    pdf2docx.exe convert test.pdf test.docx --pages=0,2,4
    

    如果pdf是加密的,可以这样转换

    pdf2docx.exe convert test.pdf test.docx --password=PASSWORD
    
  • gui

    如果你不习惯用命令行,pdf2docx也提供了一个简单的图形界面,在cmd中敲入pdf2docx gui就可以调出来。真的是很粗糙,按钮的文字都没有显示全,不过功能还是ok的。

    pdf2docx

API的使用

如果要在python中实现pdfdocx的转换,pdf2docx为我们提供了完整的api,来看一个最简单的示例

from pdf2docx import Converter

    
if __name__ == "__main__":
    
    pdf_file = "test.pdf"
    docx_file = "test.docx"

    conv = Converter(pdf_file)
    conv.convert(docx_file, start=0, end=None)
    conv.close()

更详细的API文档,可以参考链接 dothinking.github.io/pdf2docx/mo…

局限性

目前的pdf2docx版本,仅适用于基于文本的pdf,阅读习惯是从左到右。大家在使用的时候需要注意。

Python实用模块专题

更多有用的python模块,请移步

xugaoxiang.com/category/py…

参考资料