将pdf转换为文本python错误

时间:2015-06-09 13:23:49

标签: python pdftotext

我想将pdf转换为指定目录中的文本

这是我试过的代码

import os
import subprocess

def pdftotext(pdf):
    # insert your code here
    basename, _ = os.path.splitext(os.path.basename(pdf))
    subprocess.call(['pdftotext', '-enc', 'UTF-8',
                 pdf, os.path.join('c:\pdf\pydf\data', basename + '.txt')])

pdftotext("C:\\pdf\\pydf\\pdfs\\ipm.pdf")
with open(os.path.join('c:\\pdf\\pydf\\data', 'ipm.txt')) as infile:
   print(infile.read(1000))

但是收到错误

Traceback (most recent call last):
File "C:/pdf/browser.py", line 10, in <module>
pdftotext("C:\\pdf\\pydf\\pdfs\\ipm.pdf")
File "C:/pdf/browser.py", line 8, in pdftotext
pdf, os.path.join('c:\pdf\pydf\data', basename + '.txt'), '-'])
File "C:\Python34\lib\subprocess.py", line 537, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Python34\lib\subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1112, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

该文件的路径不正确,而不是C:\ pdf \ pydf \ pdfs \ ipm.pdf使用 os.path.join('c:','pdfs','pdfs','ipm.pdf')