使用tabula-py提取部分PDF表格时出现‘文件不存在’错误求助
问题解决:Tabula-Python提取特定PDF时报“文件不存在”
问题现象
执行以下代码提取PDF表格时,部分PDF触发报错:
tables = read_pdf(file_path, pages = 'all')
错误详情:
Error from tabula-java: Error: File does not exist Traceback (most recent call last): Input In [71] in <cell line: 1> tables = read_pdf(file_path, pages = 'all') File ~\anaconda3\lib\site-packages\tabula\io.py:322 in read_pdf output = _run(java_options, kwargs, path, encoding) File ~\anaconda3\lib\site-packages\tabula\io.py:80 in _run result = subprocess.run( File ~\anaconda3\lib\subprocess.py:516 in run raise CalledProcessError(retcode, process.args, CalledProcessError: Command '['java', '-Dfile.encoding=UTF8', '-jar', 'C:\\Users\\xxx\\anaconda3\\lib\\site-packages\\tabula\\tabula-1.0.5-jar-with-dependencies.jar', '--pages', 'all', '--guess', '--format', 'JSON', 'C:/Users/xx/yyy/Invoice/75211-INV-1180235.PDF']' returned non-zero exit status 1.
已知前提:
- 其他PDF文件可正常提取表格
- Tabula的GUI版本(tabula.exe)能正常处理该报错PDF
- 打印的文件路径正确,但路径包含空格和非ASCII字符(如
Kinh Doanh - Documents/Chứng Từ)
解决方案
1. 简化文件路径
将报错PDF复制到不含空格、非ASCII字符的路径下,比如C:/temp/75211-INV-1180235.PDF,再执行提取代码。
2. 转换为Windows短路径
通过命令行获取文件的8.3格式短路径,规避非ASCII和空格问题:
import subprocess import tabula def get_short_path(long_path): result = subprocess.check_output(['cmd', '/c', f'for %f in ("{long_path}") do @echo %~sf'], shell=True) return result.decode('utf-8').strip() file_path = "C:/Users/quock/tapetco/Kinh Doanh - Documents/Chứng Từ/Foreign Airports/AEG/Invoice/error/75211-INV-1180235.PDF" short_path = get_short_path(file_path) tables = tabula.read_pdf(short_path, pages='all')
3. 升级Tabula-Java版本
当前使用的tabula-1.0.5版本较旧,对非ASCII路径支持不足。手动下载最新版tabula-java jar包,替换C:\\Users\\xxx\\anaconda3\\lib\\site-packages\\tabula\\目录下的旧jar文件即可。
4. 显式指定编码与原始路径
调用read_pdf时使用原始字符串路径,并确保Java编码设置正确:
tables = tabula.read_pdf(r"C:/Users/quock/tapetco/Kinh Doanh - Documents/Chứng Từ/Foreign Airports/AEG/Invoice/error/75211-INV-1180235.PDF", pages='all', java_options=["-Dfile.encoding=UTF-8"])
原因分析
Tabula-Python依赖的底层工具Tabula-Java,对包含非ASCII字符或空格的路径存在兼容问题;而Tabula的GUI版本做了路径转义处理,因此能正常运行。
内容的提问来源于stack exchange,提问作者regulus
相关产品推荐
相关产品推荐

