首页 > 编程语言 >python系列:使用Python快速读取PDF中的表单数据以及error处理

python系列:使用Python快速读取PDF中的表单数据以及error处理

时间:2024-06-18 18:58:25浏览次数:10  
标签:读取 python 表单 content Python error PDF pdf page

使用Python快速读取PDF中的表单数据以及error处理




使用Python快速读取PDF中的表单数据

PDF表单是常见的数据收集工具,用于收集用户或客户提供的信息。通过编程的方式读取PDF表单的数据可以准确获取用户信息,避免手动输入或转录,从而节省时间和劳动力,同时降低数据输入错误的风险。这篇文章将探讨如何使用Python快速读取PDF表单数据。

安装Python PDF库

Python中有许多可以处理PDF的库,这篇文章使用的库是Spire.PDF for Python,它支持创建和读取各种类型的PDF表单,包括文本框、列表框、下拉列表(组合框)、复选框、单选按钮等。此外,还支持对PDF文档进行很多其他操作,例如合并PDF拆分PDF转换PDFWordExcel等格式。

你可以通过在终端运行以下命令来从PyPI安装Spire.PDF for Python

pip install Spire.PDF

Python读取PDF表单数据

在读取PDF文档的表单数据时,你可以选择一次性读取多个表单的数据,也可以只读取某个特定表单的数据。下面将逐一介绍这两种PDF表单数据提取场景。

1、一次性读取多种PDF表单的数据

要一次性读取PDF中多种表单的数据,你需要遍历这些表单并判断每个表单的类型,然后根据它的类型相应地获取它的数据。以下步骤展示了如何获取PDF中文本框、列表框、下拉列表(组合框)、单选按钮和复选框的名称和值:

  • 创建PdfDocument实例。

  • 使用PdfDocument.LoadFromFile()方法加载PDF文档。

  • 使用PdfDocument.Form属性获取PDF文档的表单集合。

  • 创建列表存储提取的表单数据。

  • 循环遍历表单集合中的所有表单,对于每个表单,判断其类型,并根据类型获取相应的信息。

    • 如果是文本框(PdfTextBoxFieldWidget),则获取文本框的名称和值,并将其添加到列表中。

    • 如果是列表框(PdfListBoxWidgetFieldWidget),则获取列表框的名称、选中项的值以及列表框的所有项,并将它们添加到列表中。

    • 如果是下拉列表(PdfComboBoxWidgetFieldWidget),则获取下拉列表的名称、选中项的值以及下拉列表的所有项,并将它们添加到列表中。

    • 如果是单选按钮(PdfRadioButtonListFieldWidget),则获取单选按钮的名称和选中项的值,并将它们添加到列表中。

    • 如果是复选框(PdfCheckBoxWidgetFieldWidget),则获取复选框的名称和状态(选中或未选中),并将它们添加到列表中。

  • 使用open函数创建一个文本文件,并将列表中的内容写入文件中。

from spire.pdf.common import *
from spire.pdf import *
 
# 创建 PdfDocument 类的对象
doc = PdfDocument()
# 加载 PDF 文档
doc.LoadFromFile("表单.pdf")
 
# 创建列表存储提取的表单名称和值
content = []
 
# 从文档中获取表单集合
form = doc.Form
formWidget = PdfFormWidget(form)
 
# 遍历每个表单
if formWidget.FieldsWidget.Count > 0:
    for i in range(formWidget.FieldsWidget.List.Count):
        field = formWidget.FieldsWidget.get_Item(i)
 
        # 获取文本框表单的名称和值
        if isinstance(field, PdfTextBoxFieldWidget):
            textBoxField = field
            name = textBoxField.Name
            value = textBoxField.Text
            content.append(f"文本框名称:{name}\n")
            content.append(f"文本框值:{value}\r\n")
 
        # 获取列表框表单的名称、选项和选中的项
        if isinstance(field, PdfListBoxWidgetFieldWidget):
            listBoxField = field
            name = listBoxField.Name
            content.append(f"列表框名称:{name}\n")
            content.append("列表框选项:\n")
            items = listBoxField.Values
            for i in range(items.Count):
                item = items.get_Item(i)
                content.append(f"{item.Value}\n")
            selectedValue = listBoxField.SelectedValue
            content.append(f"列表框选中项:{selectedValue}\r\n")
 
        # 获取下拉列表(组合框)表单的名称、选项和选中的项
        if isinstance(field, PdfComboBoxWidgetFieldWidget):
            comBoxField = field
            name = comBoxField.Name
            content.append(f"下拉列表名称:{name}\n")
            content.append("下拉列表选项:\n")
            items = comBoxField.Values
            for i in range(items.Count):
                item = items.get_Item(i)
                content.append(f"{item.Value}\n")
            selectedValue = comBoxField.SelectedValue
            content.append(f"下拉列表选中项:{selectedValue}\r\n")
 
        # 获取单选按钮表单的名称和选中的项
        if isinstance(field, PdfRadioButtonListFieldWidget):
            radioBtnField = field
            name = radioBtnField.Name
            content.append(f"单选按钮名称:{name}\n")
            selectedValue = radioBtnField.SelectedValue
            content.append(f"单选按钮选中项:{selectedValue}\r\n")
 
        # 获取复选框表单的名称和状态
        if isinstance(field, PdfCheckBoxWidgetFieldWidget):
            checkBoxField = field
            name = checkBoxField.Name
            content.append(f"复选框名称:{name}\n")
            status = checkBoxField.Checked
            if status:
                content.append("复选框状态:已选中\n")
            else:
                content.append("复选框状态:未选中\r\n")
 
# 将列表内容写入文本文件
with open("表单数据.txt", "w", encoding="UTF-8") as file:
    file.writelines(content)
 
doc.Dispose()

在这里插入图片描述

2、读取特定PDF表单的数据

除了一次性读取多个表单数据外,你也可以通过表单名称或它的索引获取该表单,然后获取它的数据。以下步骤展示了如何获取一个特定文本框表单的名称和值:

  • 创建PdfDocument实例。

  • 使用PdfDocument.LoadFromFile()方法加载PDF文档。

  • 使用PdfDocument.Form属性获取PDF文档的表单集合。

  • 创建列表存储提取的表单数据。

  • 通过名称或索引获取特定的文本框。

  • 获取文本框的名称和值,并将它们添加到列表中。

  • 使用open函数创建一个文本文件,并将列表中的内容写入文件中。

from spire.pdf.common import *
from spire.pdf import *
 


# 创建 PdfDocument 类的实例
doc = PdfDocument()
 
# 加载 PDF 文档
doc.LoadFromFile("表单.pdf")
 
# 创建列表以存储提取的表单名称和值
content = []
 
# 获取 PDF 表单
form = doc.Form
formWidget = PdfFormWidget(form)
 
# 通过名称获取文本框表单
field = formWidget.FieldsWidget.get_Item("姓名")
# 或者通过索引获取文本框表单
# field = formWidget.FieldsWidget.get_Item(0)
textbox = PdfTextBoxFieldWidget(field.Ptr)
 
# 获取文本框的名称和值
name = textbox.Name
value = textbox.Text
content.append(f"文本框名称: {name}\n")
content.append(f"文本框值: {value}")
 
# 将结果保存到文本文件
with open("特定表单数据.txt", "w", encoding="UTF-8") as file:
    file.writelines(content)
 
doc.Close()

在这里插入图片描述

以上代码介绍了如何使用Python从常用类型的PDF表单中提取数据,你可以根据自己PDF文档中的表单类型对代码进行扩展。

python读取PDF文件中文本、表格、图片

python读取PDF文件中文本、表格、图片

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

一、文本读取

基于fitz

import fitz
pdf_file = "example.pdf"
pdf_document = fitz.open(pdf_file)
text = ""
for page_number in range(len(pdf_document)):
    page = pdf_document.load_page(page_number)
    for block in page.get_text("blocks"):
        x0, y0, x1, y1 = block[0:4]
        text_block = block[4]
        # 根据文本块属性过滤表格中的文本
        # 这只是一个示例,你可以根据文本块的位置和其他属性来进一步过滤
        if y1 - y0 < 20:  # 通过高度过滤小文本块
            continue
        if "image" in text_block:
            continue
        text += text_block
pdf_document.close()
print(text)

二、图片读取

基于fitz

import fitz
doc = fitz.open("example.pdf") # open a document
for page_index in range(len(doc)): # iterate over pdf pages
    page = doc[page_index] # get the page
    image_list = page.get_images()
    # print the number of images found on the page
    if image_list:
        print(f"Found {len(image_list)} images on page {page_index}")
    else:
        print("No images found on page", page_index)
    for image_index, img in enumerate(image_list, start=1): # enumerate the image list
        xref = img[0] # get the XREF of the image
        pix = fitz.Pixmap(doc, xref) # create a Pixmap
        if pix.n - pix.alpha > 3: # CMYK: convert to RGB first
            pix = fitz.Pixmap(fitz.csRGB, pix)
        pix.save("page_%s-image_%s.png" % (page_index, image_index)) # save the image as png
        pix = None

三、表格读取

基于fitz

import fitz
doc = fitz.open("example.pdf") # open a document
for page_index in range(len(doc)): # iterate over pdf pages
    page = doc[page_index] # get the page
    image_list = page.get_images()
    # print the number of images found on the page
    if image_list:
        print(f"Found {len(image_list)} images on page {page_index}")
    else:
        print("No images found on page", page_index)
    for image_index, img in enumerate(image_list, start=1): # enumerate the image list
        xref = img[0] # get the XREF of the image
        pix = fitz.Pixmap(doc, xref) # create a Pixmap
        if pix.n - pix.alpha > 3: # CMYK: convert to RGB first
            pix = fitz.Pixmap(fitz.csRGB, pix)
        pix.save("page_%s-image_%s.png" % (page_index, image_index)) # save the image as png
        pix = None

基于fitz,将表格数据当作文本内容抽取

import fitz
doc = fitz.open("example.pdf") # open a document
out = open("output.txt", "wb") # create a text output
for page in doc: # iterate the document pages
    text = page.get_text().encode("utf8") # get plain text (is in UTF-8)
    out.write(text) # write text of page
    out.write(bytes((12,))) # write page delimiter (form feed 0x0C)
out.close()

基于pdfplumber

import pdfplumber
import pandas as pd
# 读取pdf文件,保存为pdf实例
pdf =  pdfplumber.open("example.pdf") 
# 访问第二页
first_page = pdf.pages[1]
# 自动读取表格信息,返回列表
tables = first_page.extract_tables(table_settings = {})
for table in tables:
    table = pd.DataFrame(table[1:], columns=table[0])
    print(table)

问题

AttributeError: ‘PdfPageBase’ object has no attribute ‘ExtractText’

解释:

这个错误表明你正在尝试在一个PdfPageBase对象上调用ExtractText方法,但是这个对象并没有这个属性或方法。这通常发生在使用PythonPyPDF2库处理PDF文件时,因为PyPDF2库中的PdfPageBase对象确实不包含提取文本的方法。

解决方法:

确保你在正确的对象上调用正确的方法。如果你想要提取PDF页面的文本,你应该在PdfPage对象上调用extract_text()方法,而不是ExtractText。以下是一个使用PyPDF2库提取PDF文本的简单示例:

import PyPDF2
 
# 打开PDF文件
with open('example.pdf', 'rb') as file:
    reader = PyPDF2.PdfFileReader(file)
 
    # 遍历PDF的每一页
    for i in range(reader.numPages):
        page = reader.getPage(i)
        print(page.extract_text())  # 提取文本

请确保你的代码中使用的是extract_text()而不是ExtractText。注意Python中的方法名通常是全小写,并且没有大写字母,除非它们是专门的类名或者首字母大写的变量名。

PyMuPDF 读取pdf时 显示 AttributeError: ‘Page‘ object has no attribute ‘getText‘ 解决方案

先上出错代码

import fitz
from tqdm import tqdm #一个遍历的读条包 可以无视
 
doc = fitz.open(input_path)
content =''
for page in tqdm(doc):
    content += page.getText('html')

这问题很简单 因为新款的PyMuPDF包getText方法更名为了 get_text 望周知!!!

所以代码更改为

import fitz
from tqdm import tqdm #一个遍历的读条包 可以无视
 
doc = fitz.open(input_path)
content =''
for page in tqdm(doc):
    content += page.get_text('html')

之后就运行正常了!

致敬我用于搜索的一个半小时!







Young_Lb

python读取PDF文件中文本、表格、图片

nuclear2011

使用Python快速读取PDF中的表单数据

Evalikepython

PyMuPDF 读取pdf时 显示 AttributeError: ‘Page‘ object has no attribute ‘getText‘ 解决方案

标签:读取,python,表单,content,Python,error,PDF,pdf,page
From: https://blog.csdn.net/weixin_54626591/article/details/139752459

相关文章

  • python系列:[Python]在VSCode中搭建Python开发环境
    [Python]在VSCode中搭建Python开发环境[Python]在VSCode中搭建Python开发环境前言安装1.安装VSCode的Python插件2.选择python解释器3.运行代码配置python检查项安装对应的库修改vscode的配置文件[Python]在VSCode中搭建Python开发环境前言之前用过Anaconda......
  • centos7 安装 python3.12
    准备工作yuminstallcentos-release-sclsclenabledevtoolset-11bashwgethttps://www.python.org/ftp/python/3.12.4/Python-3.12.4.tgz编译注意是在scl环境下进行的,gcc版本过低的环境会导致编译失败tar-zxfPython-3.12.4.tgzcdPython-3.12.4./configure--pr......
  • python文件操作、文件操作、读写文件、写模式
    with读取文件数据内容withopen(filepath,mode,encoding)asfile:#具体操作,例如:print(file.read())#查看文件所有的内容。with:Python中的一个上下文管理器,用于简化资源的管理和释放。它可以用于任意需要进行资源分配和释放的情境,比如文件操作、数据库连......
  • Win11+Miniconda3+python3.9安装pyspark+pytorch
    Win11+Miniconda3+python3.9安装pyspark+pytorch步骤1:安装Miniconda3,具体可以百度或者google步骤2:安装好Miniconda3之后,要创建虚拟环境,类似于虚拟机的样子,然后在虚拟环境安装各种python包已经装好了pytorch,具体步骤可以参考网上的一些教程,很多时候要综合多个教程,比如说先建立......
  • python 注册nacos 进行接口规范定义
    背景:一般场景python服务经常作为java下游的算法服务或者数据处理服务但是使用http去调用比较不灵活,通过注册到nacos上进行微服务调用才是比较爽的1.定义feginapi的接口定义java端定义接口请求和响应主要关注CommonResult结构和python要一直,不然序列号是有问题的Co......
  • Python编程基础:f-字符串格式
    本文探讨使用Pythonf-字符串格式,也称为“格式化字符串文字”。f-string是格式化字符串的一种很好且简单的方法,适用于Pythonv3.6+。如果你仍然使用.format()方法,必须了解f-字符串。使用字符串格式的优势之一是能够“插入”并格式化字符串数据中的变量。Python字符串format()方......
  • python调用智能合约代码,BadFunctionCallOutput 怎么解决
    目录桌面应用使用QT5开发的,可以看看我的QT5文章BadFunctionCallOutput 怎么解决我的原因是智能合约地址填写错误python智能合约基础应用如何使用remix编写solidity智能合约并部署上链在哪进行合约部署,合约部署步骤Remix怎么复制abi和address​编辑这个ABI对应最简......
  • 【python】pandas:DataFrame详解
    DataFrame是Pandas库中的一个核心数据结构,用于处理和分析表格型数据。以下是关于DataFrame的详细介绍:1.定义DataFrame是一个二维的表格型数据结构,它含有一组有序的列,每列可以是不同的值类型(数值、字符串、布尔型值等)。DataFrame可以被视为一个电子表格或SQL表,或是由多个Seri......
  • 【暑假Python上岸计划】最新20+Python实战案例,全程干货,30天看完即可接单就业!(基础+进阶
    前言今天给大家分享20+个基于python的实战案例,主要包含:数据分析、可视化、机器学习/深度学习、时序预测等,案例的主要特点:*提供源码:代码都是基于jupyternotebook,附带一定的注释,运行即可*数据齐全:大部分案例都有提供数据,部分案例使用内置数据集学习资料已打包,需要......
  • 【Python】python实现双向链表
    一、定义与结构双向链表(DoublyLinkedList)是一种链式数据结构,每个节点(Node)包含三个部分:一个数据域(data),一个指向前驱节点的指针(prev),以及一个指向后继节点的指针(next)。双向链表的每个节点都链接到前一个节点和后一个节点,从而允许在两个方向上进行遍历。双向链表的结构+---......