介绍

pdf是一种便携式文档格式,由Adobe公司设计。因为不受平台限制,且方便保存和传输,所以pdf非常受欢迎。目前市场上有很多pdf工具,大部分是阅读类,也有支持对pdf的修改、转换等功能,但这部分工具不少是收费的。这里介绍一个开源python工具库-pdfplumber,可以方便地获取pdf的各种信息,包括文本、表格、图表、尺寸等。

pdfplumber安装以及导入

  • 首先pdfplumber安装导入:和其他库一样,pdfplumber支持使用pip安装
pip install pdfplumber

pdfplumber安装完成后,用import导入使用

import pdfplumber
  • 利用.extract_tables( )可以输出pdf中的表格

pdfplumber简单使用

pdfplumber中有两个基础类,PDF和Page。前者用来处理整个文档,后者用来处理整个页面

  • pdfplumber.PDF类:
  • .metadata: 获取pdf基础信息,返回字典
  • .pages 一个包含pdfplumber.Page实例的列表,每一个实例代表pdf每一页的信息。
  • pdfplumber.Page类:pdfplumber核心功能,对PDF的大部分操作都是基于这个类,包括提取文本、表格、尺寸等
  1. 读取PDF
import pdfplumber
import pandas as pd

with pdfplumber.open("1.pdf") as pdf:
  1. 读取PDF文档信息
with pdfplumber.open("1.pdf") as pdf:
    print(pdf.metadata)

输出:
{‘Author’: ‘作者’, ‘CreationDate’: “(D:20180730130816+08’00’)”, ‘Creator’: ‘创建者’, ‘Keywords’: ‘关键字’, ‘ModDate’: “(D:20180730130816+08’00’)”, ‘Producer’: ‘生产者’, ‘Subject’: ‘科目’, ‘Title’: ‘标题’}
利用metadata可以获得PDF的基本信息,作者,日期,来源等基本信息。
3. 总页数

len(pdf.pages)
  1. 读取第一页的宽度,页高等信息
# 第一页pdfplumber.Page实例
    first_page = pdf.pages[0]

    # 查看页码
    print('页码:', first_page.page_number)

    # 查看页宽
    print('页宽:', first_page.width)

    # 查看页高
    print('页高:', first_page.height)
  1. 读取文本
import pdfplumber
import pandas as pd

with pdfplumber.open("1.pdf") as pdf:
    # print(pdf.metadata)
    # print(len(pdf.pages))
    # 第一页pdfplumber.Page实例
    first_page = pdf.pages[0]
    text = first_page.extract_text();
    print(text)

输出:

政府数据治理的国际经验与启示
夏义堃
(武汉大学信息资源研究中心,武汉,430072)
[摘 要] 政府数据治理是当前政府信息管理研究的热点问题,对发达国家政府数据治理经验的
   
总结有助于把握政府数据治理的普遍规律,推动我国政府数据的开发利用。 借助大量的文献调研
与案例分析,本文对政府数据治理的战略框架、内容体系以及生态环境等核心要素进行了深入而
全面的国际比较,系统分析了代表性国家政府数据治理实践所采取的普遍做法,进而为我国政府
数据治理实践提供启示和借鉴。
[关键词] 政府数据治理 数据开放 数据开发利用 数据安全 数据资产管理
         
[中图分类号] G203;D630 [文献标识码] A [文章编号] 2095-2171(2018)03-0064-09
   
DOI:10.13365/j.jirm.2018.03.064
InternationalExperiencesandImplicationsonGovernmentDataGovernance
XiaYikun
(CenterforStudiesofInformationResourcesofWuhanUniversity,Wuhan,430072)
[Abstract] Governmentdatagovernanceisahotissueintheresearchofgovernmentinformationmanagement.
 
Asummaryaboutexperiencesongovernmentdatagovernancefromdevelopedcountrieswillhelpustopromotethe
processofgovernmentdataexploitation.Basedonalargenumberofliteraturereviewandcasestudies,thispaper
takesaninternationalcomprehensiveandin-depthcomparisononthestrategicframework,contentsystemandeco-
logicalenvironmentofgovernmentdatagovernance,systemicallyanalyzingitsuniversalpractice.Then,itprovides
somereferencestothedevelopmentofChinesegovernmentdatagovernance.
[Keywords] Governmentdatagovernance; Opendata; Dataexploitation; Datasecurity; Dataasset
 
management
政府数据治理是政府治理和政府信息管 还是中观层面的数据法规制度,乃至微观层面
  
理的重要组成部分,“是综合运用数据管理法 的数据实践,都充分体现了政府数据治理理念
律制度、人员组织、技术方法以及流程标准等 和数据治理的行为典范,具有普遍借鉴意义。
手段,对政府结构化数据和非结构化数据的可
1 强化政府数据治理的顶层设计
用性、完整性、安全性等进行全面管理,以确保  
数字化时代政府数据量的急剧扩张与技
政府数据资产的保值增值。”[1]随着大数据战
术手段的快速更新,亟待在宏观层面实现思维
略和开放政府数据战略的全球推进,特别是对
观念从“数据管理”向“数据作为战略资产”的
数据资产价值认识的不断深化,各国纷纷出台
一系列政策举措来推进政府数据的开发利用。 转变,并需要最高决策层对政府数据治理的战
虽然并没有系统性地提出政府数据治理的概 略目标、战略重点、主攻方向以及工作机制、推
念体系,但无论是宏观层面的数据战略意识, 进方式等进行整体设计,以确定数据治理工作
[作者简介] 夏义堃,博士,研究员,副院长,研究方向为政府信息资源管理、电子政务,Email:xyk@whu.edu.cn。
  1. 读取表格
import pdfplumber
import pandas as pd

with pdfplumber.open("1.pdf") as pdf:
    page_third = pdf.pages[2]
    table_2 = page_third.extract_table()
    # print(table_2)
    table_df = pd.DataFrame(table_2[1:], columns=table_2[0])
    print(table_df)
    # 保存excel
    table_df.to_excel('test.xlsx')

pdfplumber提取表格需要处理很多细节,此处表格线框比较规范,所以可以简单提取,对于线条不完全的表格,则效果差的多。

  • 表格参数设置