当前位置:   article > 正文

Python实现PDF文件转表格_python plumber table

python plumber table

方式一:tabula-py

需要安装java

  1. #pip install tabula-py
  2. import tabula
  3. import pandas as pd
  4. df = tabula.read_pdf("D:\\我的文档\\Python\\2019221145237597.pdf",
  5. encoding='gbk', pages='all')
  6. print(df)
  7. for indexs in df.index:
  8. # 遍历打印
  9. print(df.loc[indexs].values[0:-1])
  1. import tabula
  2. # Read pdf into DataFrame
  3. df = tabula.read_pdf("test.pdf", options)
  4. # Read remote pdf into DataFrame
  5. df2 = tabula.read_pdf("https://github.com/tabulapdf/tabula-java/raw/master/src/test/resources/technology/tabula/arabic.pdf")
  6. # convert PDF into CSV
  7. tabula.convert_into("test.pdf", "output.csv", output_format="csv")
  8. # convert all PDFs in a directory
  9. tabula.convert_into_by_batch("input_directory", output_format='csv')

方式二:pdfplumber

Pdfplumber是一个可以处理pdf格式信息的库。可以查找关于每个文本字符、矩阵、和行的详细信息,也可以对表格进行提取并进行可视化调试。

https://github.com/jsvine/pdfplumber

简单使用

  1. import pdfplumber
  2. with pdfplumber.open("path/file.pdf") as pdf:
  3. first_page = pdf.pages[0] #获取第一页
  4. print(first_page.chars[0])

dfplumber.pdf中包含了.metadata和.pages两个属性。
metadata是一个包含pdf信息的字典。
pages是一个包含页面信息的列表。

每个pdfplumber.page的类中包含了几个主要的属性。
page_number 页码
width 页面宽度
height 页面高度
objects/.chars/.lines/.rects 这些属性中每一个都是一个列表,每个列表都包含一个字典,每个字典用于说明页面中的对象信息, 包括直线,字符, 方格等位置信息。

常用方法

  1. extract_text() 用来提页面中的文本,将页面的所有字符对象整理为的那个字符串
  2. extract_words() 返回的是所有的单词及其相关信息
  3. extract_tables() 提取页面的表格
  4. to_image() 用于可视化调试时,返回PageImage类的一个实例

表提取设置

默认情况下,extract_tables使用页面的垂直和水平线(或矩形边)作为单元格分隔符。但是方法该可以通过table_settings参数高度定制。可能的设置及其默认值:

  1. {
  2. "vertical_strategy": "lines",
  3. "horizontal_strategy": "lines",
  4. "explicit_vertical_lines": [],
  5. "explicit_horizontal_lines": [],
  6. "snap_tolerance": 3,
  7. "join_tolerance": 3,
  8. "edge_min_length": 3,
  9. "min_words_vertical": 3,
  10. "min_words_horizontal": 1,
  11. "keep_blank_chars": False,
  12. "text_tolerance": 3,
  13. "text_x_tolerance": None,
  14. "text_y_tolerance": None,
  15. "intersection_tolerance": 3,
  16. "intersection_x_tolerance": None,
  17. "intersection_y_tolerance": None,
  18. }

举例使用

读取文字

  1. import pdfplumber
  2. import pandas as pd
  3. with pdfplumber.open("E:\\600aaa_2.pdf") as pdf:
  4. page_count = len(pdf.pages)
  5. print(page_count) # 得到页数
  6. for page in pdf.pages:
  7. print('---------- 第[%d]页 ----------' % page.page_number)
  8. # 获取当前页面的全部文本信息,包括表格中的文字
  9. print(page.extract_text())

读取表格

  1. import pdfplumber
  2. import pandas as pd
  3. import re
  4. with pdfplumber.open("E:\\600aaa_1.pdf") as pdf:
  5. page_count = len(pdf.pages)
  6. print(page_count) # 得到页数
  7. for page in pdf.pages:
  8. print('---------- 第[%d]页 ----------' % page.page_number)
  9. for pdf_table in page.extract_tables(table_settings={"vertical_strategy": "text",
  10. "horizontal_strategy": "lines",
  11. "intersection_tolerance":20}): # 边缘相交合并单元格大小
  12. # print(pdf_table)
  13. for row in pdf_table:
  14. # 去掉回车换行
  15. print([re.sub('\s+', '', cell) if cell is not None else None for cell in row])

 案例

  1. import pandas as pd
  2. def to_table(pdf_table):
  3. #将DataFrame第一行作为表头
  4. df=pd.DataFrame(pdf_table)
  5. df.columns = df.iloc[0]
  6. df=df.drop(df.index[0])
  7. return df
  1. import pdfplumber
  2. import pandas as pd
  3. import re
  4. with pdfplumber.open("/mnt/c/Users/admin/Downloads/202104291855528(file)附件:天津市医保药品支付范围信息维护明细表(2021年第四期).pdf") as pdf:
  5. page_count = len(pdf.pages)
  6. print(page_count) # 得到页数
  7. i=0
  8. for page in pdf.pages:
  9. print('---------- 第[%d]页 ----------' % page.page_number)
  10. for pdf_table in page.extract_tables(table_settings={"vertical_strategy": "text",
  11. "horizontal_strategy": "lines",
  12. "intersection_tolerance":20}): # 边缘相交合并单元格大小
  13. # table=pd.DataFrame(pdf_table)
  14. table=to_table(pdf_table)
  15. table['PDF页码']='第[{}]页'.format(page.page_number)
  16. print(table)
  17. i=i+1
  18. if i==1:
  19. ddf=table
  20. else:
  21. ddf=pd.concat([ddf, table])
  22. ddf.to_excel('/mnt/c/Users/admin/Downloads/202104291855528(file)附件:天津市医保药品支付范围信息维护明细表(2021年第四期).xlsx',index=False)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/51339
推荐阅读
相关标签
  

闽ICP备14008679号