Python pdf转word

最新在翻译英文文档,但都是pdf的,有点不方便,花了点时间做了一个小工具,分享一下,希望对大家有所帮助。

这里录了一个视频传到B站了,比较详细可以看一下:传送门。

最终结果是生成了一个可执行文件,可以批量转换文件夹中的pdf文件,包含图片和简单的格式转换(复杂的就不用考虑自己搞了QAQ)

下面简单描述一下大概思路:

1.引用pdf2docx库:

from pdf2docx import Converter

2.找到当前文件夹下后缀是pdf的文档,依次转换一下

    for file in os.listdir(os.getcwd()):
        #获取文件的后缀
        extension_name = os.path.splitext(file)[1]
        #判断是否是pdf文件
        if extension_name != '.pdf':
            continue
        #获取文件名  生成的word文件名与PDF一致
        file_name = os.path.splitext(file)[0]
        pdf_file = os.getcwd() + '/' + file
        word_file = os.getcwd() + '/' + file_name + '.docx'
        
        #加载并转换
        cv = Converter(pdf_file)
        cv.convert(word_file)
        cv.close()

完整代码:

from pdf2docx import Converter
import os


def main():

    for file in os.listdir(os.getcwd()):
        #获取文件的后缀
        extension_name = os.path.splitext(file)[1]
        #判断是否是pdf文件
        if extension_name != '.pdf':
            continue
        #获取文件名  生成的word文件名与PDF一致
        file_name = os.path.splitext(file)[0]
        pdf_file = os.getcwd() + '/' + file
        word_file = os.getcwd() + '/' + file_name + '.docx'
        
        #加载并转换
        cv = Converter(pdf_file)
        cv.convert(word_file)
        cv.close()


if __name__ == '__main__':
    main()

最后用pyinstaller 生成可执行文件,就可以任意地方跑起来了QWQ

pyinstaller -F fileName.py

你可能感兴趣的:(python)