我遇到一个问题,需要像Adobe Acrobat的工作方式一样将pdf文档转换为OCR pdf文档。我已经尝试过使用ocrmypdf模块,但是不知何故。我正在使用python 2.7。任何其他模块也将受到赞赏。
import logging
import os
import subprocess
import sys
import time
import shutil
path="D:\Nikhil Scraping\Pdf all processing"
for filenames in os.listdir(path):
print (filenames)
filename=filenames.split('.')[0]
print (filename)
input_path=os.path.join(path,filenames)
outputfile=filename+"_OCR.pdf"
cmd=["ocrmypdf","--output-type", "pdf", input_path, outputfile]
logging.info(cmd)
proc=subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
result = proc.stdout.read()
显示的错误:
1-9-US 118137380VP1.pdf
1-9-US 118137380VP1
Traceback (most recent call last):
File "D:\Nikhil Scraping\Pdf all processing\pdf_ocr_working.py", line 19, in <module>
proc=subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
File "C:\Python27\Lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "C:\Python27\Lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
当我在python 3.7中使用相同的代码时,它工作正常,但未生成任何输出文件。
它也可以在Mac OS中成功运行,我不知道为什么Windows会显示此错误。
答案 0 :(得分:0)
您要在此处加入所有文件名,而不是一个文件名
input_path=os.path.join(path,filenames)
改为使用此代码
input_path=os.path.join(path,filename)