【问题标题】:PDF parsing: using pdfminer and pandasPDF解析:使用pdfminer和pandas
【发布时间】:2023-03-09 12:31:01
【问题描述】:

我正在尝试将 pdf 文件解析为 csv 格式。 pdf中,有一个没有边框的表格,所以here建议的方法不起作用。我的想法是用pdfminer分析pdf的布局,定位所有textlines,匹配每个textlines的bbox位置,重构表格。

到目前为止,通过比较每个文本行对象的 x0 坐标,我已经成功地将文本行分类到“左”和“右”列,我将根据它们的 y0 坐标匹配左右行。当我试图将每一行的内容放入 pandas DataFrame 时,我得到一个 TypeErrorL cannot concatenate a non-NDFrame object。请帮忙。

我的代码如下:

testfile = 'file location'
page_layouts = extract_layout_by_page(testfile)
l_lines = []
r_lines = []
for elem in page_layouts[0]:
    if isinstance(elem, pdfminer.layout.LTTextBoxHorizontal):
       for l in elem:
           (x0,y0,x1,y1) = l.bbox
           if x0 <= 65.35 and x0 >=65.33:
               l_lines.append(l)
           elif x0 <= 280.1 and x0 >= 279.9:
               r_lines.append(l)

csv = pd.DataFrame()
csv['l'] = 0
csv['r'] = 0

for i in r_lines:
    x = i.get_text().encode('ascii','ignore')
    csv['r'].append(x) 

提前谢谢你。

【问题讨论】:

    标签: python pandas pdf pdfminer


    【解决方案1】:

    它很旧,但也许它会对某人有所帮助。我猜你的错误是将文本添加到数据框中。你应该把这个文本放在一个系列中。 你这样做是最新的部分:

    l=[]
    for i in r_lines:
        x = i.get_text().encode('ascii','ignore')
        l.append(x) 
    df=pd.DataFrame()
    
    #if you want to append vertically (adding a column):
    df.append(l)
    #if you want to append horizontally (adding a row):
    s=pd.Series(l)
    s.name ("Series") #you may custom format this to create a new name for each row added, with a counter for example. Giving a name is necessary when the series is appended to the df, else it raises a TypeError (unless you use ignore_index = True
    df.append(s)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多