欢迎访问我的网站,希望内容对您有用,感兴趣的可以加入免费知识星球。

Python实用模块(二十八)pdf2docx

实用模块 迷途小书童 3年前 (2021-08-17) 3561次浏览 0个评论

环境

  • 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 
    
    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
      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 文档,可以参考链接 https://dothinking.github.io/pdf2docx/modules.html

    局限性

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

    Python实用模块专题

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

    https://xugaoxiang.com/category/python/modules/

    参考资料

    喜欢 (0)

    您必须 登录 才能发表评论!