PDFplumber提取表格时遗漏最右侧列的问题求助
PDFplumber提取表格遗漏最右侧列的问题求助
在Windows 11系统下使用pdfplumber提取表格数据时,多数场景效果良好,但针对特定PDF文件,extract_table方法会遗漏表格的最右侧列,不过page.search方法能够定位到该列的内容。以下是测试代码、运行输出及相关信息,恳请提供解决线索。
测试代码
# pdftesting.py import pdfplumber import sys print('pdfplumber version:', pdfplumber.__version__) print('Python version:', sys.version) filepath = 'C:/ProgramData/PythonProgs/testing/testdoc.pdf' fn = pdfplumber.open(filepath) page = fn.pages[0] vlines = [26.0, 106.25, 152.25, 251.25, 395.5, 467.25, 539.5, 624.65, 692.5, \ 760.15, 818.9811199999999] imagefile = 'C:/ProgramData/PythonProgs/testing/testdoc.png' im = page.to_image(resolution=300) im.draw_vlines(vlines, stroke_width=3) im.save(imagefile) lines = page.extract_table(table_settings=\ {"vertical_strategy":"explicit",\ "explicit_vertical_lines":vlines,\ "horizontal_strategy": 'text',\ "snap_tolerance": 5}) for item in lines: print('line:', item) print('page width:', page.width) target = 'inc' X0 = page.search(target)[0]['x0'] X1 = page.search(target)[0]['x1'] size = page.search(target)[0]['chars'][0]['size'] print('Found:', target, X0, X1, size)
代码运行输出
pdfplumber version: 0.11.0 Python: 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] line: ['', '', '', 'tne minute, rou', 'naea up to tn', 'e nearest mi', 'nute', '', ''] line: ['UK calls', '', '', '', '', '', '', '', ''] line: ['', '', '', '', '', '', '', '', ''] line: ['Date', 'Time', 'Phone number', 'Destination', 'Duration', 'Charged', 'Included?', 'VAT', 'VAT'] line: ['', '', '', '', 'hh:mm:ss', 'hh:mm:ss', '', 'ex', 'rate'] line: ['', '', '', '', '', '', '', '', ''] line: ['Sun 17 May', '15:55', '07755221961', 'UK mobile', '00:05:26', '00:05:26', 'Yes', '£0.000', '20%'] line: ['', '', '', '', '', '', '', '', ''] line: ['Thu 21 May', '11:15', '07818818242', 'Vodafone mobile', '00:00:07', '00:01:00', 'Yes', '£0.000', '20%'] line: ['', '', '', '', '', '', '', '', ''] line: ['Fri 22 May', '15:44', '05706000459', 'Landline', '00:00:04', '00:01:00', 'Yes', '£0.000', '20%'] line: ['', '', '', '', '', '', '', '', ''] line: ['Mon 25 May', '20:48', '02085462206', 'Landline', '00:15:12', '00:15:12', 'Yes', '£0.000', '20%'] line: ['', '', '', '', '', '', '', '', ''] line: ['Sat 50 May', '10:58', '02056549856', 'Landline', '00:00:08', '00:01:00', 'Yes', '£0.000', '20%'] line: ['', '', '', '', '', '', '', '', ''] line: ['Fri 5 Jun', '09:58', '07818818242', 'Vodafone mobile', '00:00:11', '00:01:00', 'Yes', '£0.000', '20%'] line: ['', '', '', '', '', '', '', '', ''] line: ['Sat 6 Jun', '07:17', '07716065665', 'Vodafone mobile', '00:01:14', '00:01:14', 'Yes', '£0.000', '20%'] line: ['', '', '', '', '', '', '', '', ''] line: ['', '', '', 'Tot', 'al of 7 calls', '23 mins 52 s', '', '£0.000', ''] page width: 856.800048828 Found: inc 761.15 773.9811199999999 9.961000000000013
相关资料
- 测试用PDF文档
- PDF截图:展示原表格布局的截图
- 调试截图:标注了显式竖线位置的调试截图
内容的提问来源于stack exchange,提问作者PMSK
相关产品推荐
相关产品推荐

