pdfplumber简介

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

文档参考https://github.com/jsvine/pdfplumber

pdfplumber安装

安装直接采用pip即可。命令行中输入

pip install pdfplumber

如果要进行可视化的调试,则需要安装ImageMagick。
Pdfplumber GitHub: https://github.com/jsvine/pdfplumber
ImageMagick地址:
http://docs.wand-py.org/en/latest/guide/install.html#install-imagemagick-windows
(官网地址没有6x, 6x地址:https://imagemagick.org/download/binaries/)

(注意:我在装ImageMagick,使用起来是报错了, 网上参照了这里 了解到应该装6x版,7x版会报错。故找了6x的地址如上。)

在使用to_image函数输出图片时,如果报错DelegateException。则安装GhostScript 32位。(注意,一定要下载32位版本,哪怕Windows和python的版本是64位的。)
GhostScript: https://www.ghostscript.com/download/gsdnld.html

简单使用

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

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

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

常用方法

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

常用参数

table_settings

表提取设置

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

{
"vertical_strategy": "lines",
"horizontal_strategy": "lines",
"explicit_vertical_lines": [],
"explicit_horizontal_lines": [],
"snap_tolerance": 3,
"join_tolerance": 3,
"edge_min_length": 3,
"min_words_vertical": 3,
"min_words_horizontal": 1,
"keep_blank_chars": False,
"text_tolerance": 3,
"text_x_tolerance": None,
"text_y_tolerance": None,
"intersection_tolerance": 3,
"intersection_x_tolerance": None,
"intersection_y_tolerance": None,
}

表提取策略

vertical_strategy 和 horizontal_strategy 的参数选项

"lines" Use the page's graphical lines — including the sides of rectangle objects — as the borders of potential table-cells.
"lines_strict" Use the page's graphical lines — but not the sides of rectangle objects — as the borders of potential table-cells.
"text" For vertical_strategy: Deduce the (imaginary) lines that connect the left, right, or center of words on the page, and use those lines as the borders of potential table-cells. For horizontal_strategy, the same but using the tops of words.
"explicit" Only use the lines explicitly defined in explicit_vertical_lines / explicit_horizontal_lines.

举例使用

读取文字

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

读取表格

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

部分参照:https://blog.csdn.net/Elaine_jm/article/details/84841233

python读取pdf文件的更多相关文章

  1. 记一次为解决Python读取PDF文件的Shell操作

    目录 一.背景 二.问题 三.解决 四.一顿分析及 Shell 操作 五.后续 一.背景 本想将 PDF 文件转换为 Word 文档,然后网上搜索了一下发现有挺多转换的软件.有的是免费的.收费,咱也不 ...

  2. 深入学习python解析并读取PDF文件内容的方法

    这篇文章主要学习了python解析并读取PDF文件内容的方法,包括对学习库的应用,python2.7和python3.6中python解析PDF文件内容库的更新,包括对pdfminer库的详细解释和应 ...

  3. Python读取PDF内容

    1,引言 晚上翻看<Python网络数据采集>这本书,看到读取PDF内容的代码,想起来前几天集搜客刚刚发布了一个抓取网页pdf内容的抓取规则,这个规则能够把pdf内容当成html来做网页抓 ...

  4. Python绘制PDF文件~超简单的小程序

    Python绘制PDF文件 项目简介 这次项目很简单,本次项目课,代码不超过40行,主要是使用 urllib和reportlab模块,来生成一个pdf文件. reportlab官方文档 http:// ...

  5. Python读取txt文件

    Python读取txt文件,有两种方式: (1)逐行读取 data=open("data.txt") line=data.readline() while line: print ...

  6. Python读取Yaml文件

    近期看到好多使用Yaml文件做为配置文件或者数据文件的工程,随即也研究了下,发现Yaml有几个优点:可读性好.和脚本语言的交互性好(确实非常好).使用实现语言的数据类型.有一个一致的数据模型.易于实现 ...

  7. python读取中文文件编码问题

    python 读取中文文件后,作为参数使用,经常会遇到乱码或者报错asii错误等. 我们需要对中文进行decode('gbk') 如我有一个data.txt文件有如下内容: 百度 谷歌 现在想读取文件 ...

  8. Python读取SQLite文件数据

    近日在做项目时,意外听说有一种SQLite的数据库,相比自己之前使用的SQL Service甚是轻便,在对数据完整性.并发性要求不高的场景下可以尝试! 1.SQLite简介: SQLite是一个进程内 ...

  9. Python读取xlsx文件

    Python读取xlsx文件 脚本如下: from openpyxl import load_workbook workbook = load_workbook(u'/tmp/test.xlsx') ...

随机推荐

  1. hid.dll

    hid.dll是USB的HID相关动态链接库文件,缺少它可能会造成usb设备无法正常使用.当你的电脑弹出提示“计算机缺少hid.dll”或“无法找到hid.dll Hkapi.dll HKComman ...

  2. dt常用类

    经常使用的一些datatable的操作,包括一些过滤去重的方法 using System; using System.Collections; using System.Collections.Gen ...

  3. Altium Designer 16 问题解决

    1:同一个工程中,不同原理图里的网络标号不能关联起来 解决--->   选择  工程->工程参数->网络识别符范围 -> GLOBAL 2:PCB中影藏显示相应Net的飞线 解 ...

  4. String StringBuilder 包装类

    1. String 概述 程序中直接写上双引号的字符串就在字符串常量池中,new的不在池当中 java6之前常量池在方法区,java7以后将字符串常量池放在堆中 因为字符串是对象,应该在堆中 相同的字 ...

  5. CommonsChunkPlugin VS SplitChunksPlugin

    等了好久终于等到你, webpack团队人员卧薪尝胆五个多月的时间终于带来的webpack4.0,个人觉得webpack4带来的最大优化便是对于懒加载块拆分的优化,删除了CommonsChunkPlu ...

  6. 【机器学习】主成分分析法 PCA (II)

    主成分分析法(PAC)的优化——选择主成分的数量 根据上一讲,我们知道协方差为① 而训练集的方差为②. 我们希望在方差尽可能小的情况下选择尽可能小的K值. 也就是说我们需要找到k值使得①/②的值尽可能 ...

  7. MySQL远程连接失败(错误码:2003)

    一 环境信息 服务器系统:Ubuntu 18.04 服务器MySQL版本:14.14 Distrib 5.7.25 本地系统:Kali Linux 本地客户端:python3交互模式 本地开发环境:p ...

  8. vue-element 动态单选多选全选

    实现效果如图 数据格式如下: pps: [{"code":"6","createTime":"2018-09-07 00:00:0 ...

  9. redis_列表对象

    <Redis设计与实现>中说:redis列表对象有两种底层编码格式:ziplist.linkedlist,其中ziplist用压缩列表实现.linkedlist用双向链表实现 但我在实践中 ...

  10. Java类更改常量后编译不生效

    在Java文件中,指向编译时static final的静态常量, 会被在运行时解析为一个局部的常量值(也就是说静态常量在编译后,成为了常量,而不是原先的代码).这对所有的基础数据类型(就像int ,f ...