Python 将所有PDF文件转换为目录中的文本

Python 将所有PDF文件转换为目录中的文本,python,Python,我刚下载了PDF文件转换成文本。我通过在终端上执行此命令来转换文件 python pdf2txt.py -o myOutput.txt simple1.pdf 它工作得很好,现在我想在我的简单Python脚本中嵌入该函数。我想转换目录中的所有PDF文件 # Lets say I have an array with filenames on it files = [ 'file1.pdf', 'file2.pdf', 'file3.pdf' ] # And convert all P

我刚下载了PDF文件转换成文本。我通过在终端上执行此命令来转换文件

python pdf2txt.py -o myOutput.txt simple1.pdf
它工作得很好,现在我想在我的简单Python脚本中嵌入该函数。我想转换目录中的所有PDF文件

# Lets say I have an array with filenames on it
files = [
    'file1.pdf', 'file2.pdf', 'file3.pdf'
]

# And convert all PDF files to text
# By repeatedly executing pdf2txt.py
for x in range(0, len(files))
    # And run something like
    python pdf2txt.py -o output.txt files[x]

我还尝试使用
os.system
,但出现了一个闪烁的窗口(我的终端)。我只想将数组中的所有文件转换为文本。

使用
子流程
模块

import subprocess

files = [
    'file1.pdf', 'file2.pdf', 'file3.pdf'
]
for f in files:
    cmd = 'python pdf2txt.py -o %s.txt %s' % (f.split('.')[0], f)
    run = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = run.communicate()

    # display errors if they occur    
    if err:
        print err

有关详细信息,请阅读。

有一个API可帮助您执行此类任务