python提取pdf表格到excel:拆分、提取、合并

本文介绍使用python提取pdf中的表格到excel中,包含pdf的拆分、pdf提取到excel、合并excel。

一、拆分pdf

将一个大的pdf按页数拆分为多个小的pdf:

py 复制代码
# pip install PyPDF2

import os, pdfplumber, PyPDF2

# 分割pdf
def split_pdf(input_pdf_path, num_splits):
    # Create a PDF reader object
    pdf_reader = PyPDF2.PdfReader(open(input_pdf_path, 'rb'))
    total_pages = len(pdf_reader.pages)
 
    # Calculate the number of pages per split
    pages_per_split = total_pages // num_splits
 
    # Get the directory and base name of the input PDF
    base_dir = os.path.dirname(input_pdf_path)
    base_name = os.path.splitext(os.path.basename(input_pdf_path))[0]
 
    for i in range(num_splits):
        pdf_writer = PyPDF2.PdfWriter()
        start_page = i * pages_per_split
        end_page = start_page + pages_per_split
        if i == num_splits - 1:  # Make sure to include remaining pages in the last split
            end_page = total_pages
 
        for page in range(start_page, end_page):
            pdf_writer.add_page(pdf_reader.pages[page])
 
        output_pdf_path = os.path.join(base_dir, f"{base_name}_part_{i}.pdf")
        with open(output_pdf_path, 'wb') as output_pdf:
            pdf_writer.write(output_pdf)
 
    print(f"PDF split into {num_splits} parts successfully.")

二、提取pdf中的表格到excel中

将pdf表格提取到excel表格中:

py 复制代码
# pip install pdfplumber
# pip install openpyxl
 
import os, pdfplumber
from openpyxl import Workbook, load_workbook

# pdf转为xlsx
def analysis_table(pdf_file_name):
    # 打开表格
    workbook = Workbook()
    sheet = workbook.active
 
    num = 0

    # 打开pdf
    with pdfplumber.open(pdf_file_name + '.pdf') as pdf:
        # 遍历每页pdf 
        for page in pdf.pages:
            # 提取表格信息
            tables = page.extract_tables()
            
            # 遍历提取到的所有表格
            for table in tables:
                # 格式化表格数据
                for row in table:
                    sheet.append(row)

            num = num + 1
            print("process page: %d"%(num))

        workbook.save(filename=(pdf_file_name + ".xlsx"))

三、合并多个excel表格到一个excel中

py 复制代码
# pip install pdfplumber
# pip install openpyxl
 
import os, pdfplumber
from openpyxl import Workbook, load_workbook

# 合并excel文件
def merge_excels(files, output_name):
    # 加载第一个Excel文件
    wb1 = load_workbook(files[0] + '.xlsx')
    sheet1 = wb1.active

    file_list = files[1::1]
    for file in file_list:
        # 加载第二个Excel文件
        wb2 = load_workbook(file + '.xlsx')
        sheet2 = wb2.active
        
        # 合并两个工作表的数据
        for row in sheet2.iter_rows(values_only=True):
            sheet1.append(row)
     
    # 保存合并后的Excel文件
    wb1.save(output_name + '.xlsx')

四、完整代码

完整代码在:使用python实现pdf表格转为excel表格

相关推荐
XIE39236 分钟前
Browser-use使用教程
python
酷爱码2 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
蹦蹦跳跳真可爱5892 小时前
Python----深度学习(基于深度学习Pytroch簇分类,圆环分类,月牙分类)
人工智能·pytorch·python·深度学习·分类
MinggeQingchun5 小时前
Python - 爬虫-网页解析数据-库lxml(支持XPath)
爬虫·python·xpath·lxml
Python自动化办公社区6 小时前
Python 3.14:探索新版本的魅力与革新
开发语言·python
weixin_贾7 小时前
最新AI-Python机器学习与深度学习技术在植被参数反演中的核心技术应用
python·机器学习·植被参数·遥感反演
张槊哲7 小时前
函数的定义与使用(python)
开发语言·python
船长@Quant7 小时前
文档构建:Sphinx全面使用指南 — 实战篇
python·markdown·sphinx·文档构建
偶尔微微一笑8 小时前
AI网络渗透kali应用(gptshell)
linux·人工智能·python·自然语言处理·编辑器
Sherlock Ma9 小时前
PDFMathTranslate:基于LLM的PDF文档翻译及双语对照的工具【使用教程】
人工智能·pytorch·语言模型·pdf·大模型·机器翻译·deepseek