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)
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Python的第三方库PyPDF2或者pdfminer来读取PDF文件表格。其,PyPDF2可以读取PDF文件文本内容,而pdfminer可以解析PDF文件的结构信息,包括表格。可以根据具体需求选择合适的库进行操作。 ### 回答2: 要实现Python读取PDF文件表格,可以使用pdfplumber库。以下是实现的步骤: 1. 首先,安装pdfplumber库。可以使用pip命令在终端或命令提示符运行以下命令安装: ``` pip install pdfplumber ``` 2. 导入pdfplumber库。 ```python import pdfplumber ``` 3. 使用pdfplumber打开一个PDF文件。 ```python with pdfplumber.open("file.pdf") as pdf: # 读取第一页 first_page = pdf.pages[0] ``` 4. 通过extract_table()方法提取表格。 ```python # 提取表格 table = first_page.extract_table() # 打印表格内容 for row in table: print(row) ``` 提取出的表格将被存储为一个嵌套列表,其每个元素是表格的一行。你可以根据需要进行进一步的处理和分析。 5. 运行程序并读取PDF表格的数据。 完整的代码示例如下: ```python import pdfplumber with pdfplumber.open("file.pdf") as pdf: # 读取第一页 first_page = pdf.pages[0] # 提取表格 table = first_page.extract_table() # 打印表格内容 for row in table: print(row) ``` 请确保将 "file.pdf" 替换为你要读取PDF文件的路径。 ### 回答3: Python可以使用第三方库来实现对PDF文件表格读取和提取数据。 其一个常用的库是`tabula-py`,它是基于Java的`Tabula`提供了Python接口。`tabula-py`可以解析PDF表格,并将其转换为pandas DataFrame。以下是一个使用`tabula-py`读取PDF文件表格的示例代码: ``` import tabula # 使用tabula库读取PDF文件表格 df = tabula.read_pdf('file.pdf', pages='all') # 打印提取出的PDF表格内容 print(df) ``` 在上述代码,`read_pdf`函数用于读取PDF文件表格,并接收`pages`参数用于指定读取的页面范围(若不指定,则默认为读取所有页面)。读取后的数据将被转换为`pandas DataFrame`格式,并赋值给变量`df`。最后,使用`print`函数打印出提取出的PDF表格内容。 需要注意的是,`tabula-py`并不能处理所有的PDF文件。对于复杂的或被图片扫描的PDF文件,`tabula-py`可能无法正确处理表格。在这种情况下,可能需要使用更复杂的PDF解析库,如`PyPDF2`或`pdfplumber`。 除了`tabula-py`,还有其他一些PDF处理库,如`PyPDF2`和`pdfplumber`,也可以用于读取PDF文件表格。这些库提供了更多的灵活性和功能,但在使用上可能需要一些更复杂的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值