Azure函数应用中pdf2text无法正常工作问题求助
解决Azure函数应用中textract无法调用pdf2txt.py的问题
问题场景
使用textract编写了读取PDF内容的脚本,本地运行正常,但部署到Azure函数应用后报错,提示pdf2txt.py未安装。虽然requirements.txt中已添加textract和pdf2text,但依赖未正确生效。
脚本代码
import textract import tempfile def read_file(bytes): with tempfile.NamedTemporaryFile('wb', delete=True) as temp: temp.write(bytes) temp.flush() context = textract.process(temp.name, encoding='utf-8',extension=".pdf") return context.decode('utf-8')
错误信息
pdf2txt.py /tmp/tmpe3yo9gax` failed because the executable `pdf2txt.py` is not installed on your system. Please make sure the appropriate dependencies are installed before using textract: http://textract.readthedocs.org/en/latest/installation.html
解决方案
修正依赖包名称:
pip中的pdf2text并非textract处理PDF所需的依赖,textract实际依赖的是pdfminer.six。修改requirements.txt,将pdf2text替换为pdfminer.six,内容如下:textract pdfminer.six指定PDF处理方法:在调用
textract.process时显式指定method='pdfminer',强制textract使用pdfminer.six提供的工具,避免自动查找错误的可执行文件。修改后的代码片段:context = textract.process(temp.name, encoding='utf-8', extension=".pdf", method='pdfminer')清理缓存重新部署:删除Azure函数应用的旧部署包,重新触发部署,确保新的依赖包被正确安装。
内容的提问来源于stack exchange,提问作者Jeroen Vermunt
相关产品推荐
相关产品推荐

