pdf文本与表格提取

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time      :2023/2/24 13:06
# @Author    :jijfurhg
import re

# 提取pdf文字
import pdfplumber


def get_one_page_text(path, page=0):
    """
    读取pdf单页的文本
    :param path: pdf路径
    :param page: 页码,默认为第1页
    :return: 文本
    """
    with pdfplumber.open(path) as pdf:
        page01 = pdf.pages[page]  # 指定页码
        text = page01.extract_text()  # 提取文本
    return text


def get_all_text(path):
    """
    读取pdf的所有文本
    :param path: pdf路径
    :return: 文本
    """
    with pdfplumber.open(path) as pdf:
        for page in pdf.pages:
            text = page.extract_text()  # 提取文本
    return text


def get_table(path, page=0):
    """
    提取pdf单页内的表格
    :param path: pdf路径
    :param page: 页码,默认为0
    :return: 表格
    """
    with pdfplumber.open(path) as pdf:
        page01 = pdf.pages[page]  # 指定页码
        table1 = page01.extract_table()  # 提取单个表格
        # table2 = page01.extract_tables()#提取多个表格
    return table1
posted @ 2023-08-26 12:32  jijfurhg  阅读(26)  评论(0)    收藏  举报