当前位置: 首页 > news >正文

python实现PDF表格与文本分别导出EXCEL

现需将pdf 转换至Excel ,
目前实现方式:将PDF的TABLE部分与 非 TABLE部分分别导出至Excel两个sheet中
1)、识别PDF中的表格块
2)、将PDF转换为Word格式
3)、提取Word中非表格的文本数据
4)、对文本与表格重复的行进行去重
5)、合并导出至Excel不同sheet页中

# coding=UTF8
import datetime
from docx import Document
from pdf2docx import Converter
import pandas as pd
import numpy as np
import pdfplumber
import os
import fitz# TODO 输出PDF表格数据至Excel
def extractTables(filepath):with pdfplumber.open(filepath) as pdf:tables = []for i in range(0, len(pdf.pages)):page = pdf.pages[i]tables.append(page.extract_tables())df = pd.DataFrame()df_seperation = pd.DataFrame([np.nan, np.nan])  # 创建空白的,用于充当分隔行for i in range(0, len(tables)):tabular = tables[i]  # 选取第i页的表格if len(tabular) > 0:  # 如果该页存在表格的话for j in range(0, len(tabular)):  # j  表示第几个表格df_temp = pd.DataFrame(tabular[j])df = pd.concat([df, df_seperation, df_temp])  # 更新总表格return df# TODO pdf 转Word
def extractWord(pdffilepath, wordfilepath):cv = Converter(pdffilepath)cv.convert(wordfilepath)cv.close()# TODO 获取非表格内容
def getDocLines(wordfilepath):doc = Document(wordfilepath)paragraphs = doc.paragraphslines = []for paragraph in paragraphs:line = paragraph.text.strip()if not line:continuelines.append(line)# aspose用的体验板,带有页眉# lines = lines[100:]# print(lines)df = pd.DataFrame(lines)return df# TODO 删除与table重复的行数据
def txt(tabledf, txtdf):lines = []for line in tabledf[0]:lines.append(str(line))# 获取txt与Excel重复的数据repeat_txt = []for line in lines:for txt in txtdf[0]:if line.find(txt) != -1:repeat_txt.append(txt)txtdf = txtdf.drop(txtdf[txtdf[0] == txt].index)return txtdfif __name__ == '__main__':filepath = 'D:\develop_python\Python_Demo\PDF_TO_EXCEL\YM2021\\'outpath = 'D:\develop_python\Python_Demo\PDF_TO_EXCEL\YM2021\\'pdffile = '南通-2.21'pdffilepath = filepath + str(pdffile) + '.pdf'excelfilepath = filepath + str(pdffile) + '.xlsx'wordfilepath = filepath + str(pdffile) + '.docx'# 获取PDF表格数据try:starttime = datetime.datetime.now()print('执行开始', starttime)if not os.path.exists(outpath):os.makedirs(outpath)print()print('正在读取表格数据........')tabledf = extractTables(pdffilepath)print('表格数据读取完成........')print()print('正在转换Word......')extractWord(pdffilepath, wordfilepath)print('Word转换完成......')print()print('正在解析非表格文本数据......')txtdf = getDocLines(wordfilepath)# 删除与table重复的行数据df = txt(tabledf, txtdf)print('非表格数据解析完成......')print()print('正在输出Excel文件......')writer = pd.ExcelWriter(excelfilepath)tabledf.to_excel(writer, sheet_name='EXCEL', index=False)# txtdf.to_excel(writer, sheet_name='txt', index=False)df.to_excel(writer, sheet_name='txt', index=False)writer.save()print('Excel文件输出成功......')print()endtime = datetime.datetime.now()print('执行结束', endtime)print('耗时', endtime - starttime)except Exception  as e:print(Exception, e.args)
http://www.lryc.cn/news/206076.html

相关文章:

  • Java开发-WebSocket
  • SpringDoc API文档工具集成SpringBoot - Swagger3
  • Java将djvu文件转成pdf
  • 【机器学习合集】激活函数合集 ->(个人学习记录笔记)
  • 【从0到1设计一个网关】什么是网关?以及为什么需要自研网关?
  • Tp框架如何使用事务和锁,还有查询缓存
  • Java IDEA feign调用上传文件MultipartFile以及实体对象亲测可行
  • 【产品经理】APP备案(阿里云)
  • Overmind VS Redux
  • 0基础学习PyFlink——流批模式在主键上的对比
  • Java学习笔记(五)——数组、排序和查找
  • python输出与数据类型
  • React-Redux总结含购物车案例
  • 攻克组合优化问题!美国DARPA选中全栈量子经典计算公司Rigetti
  • Kafka - 深入了解Kafka基础架构:Kafka的基本概念
  • [Docker]二.Docker 镜像,仓库,容器介绍以及详解
  • 软考高级系统架构设计师系列之:案例分析典型试题一
  • 2023年5个美国代理IP推荐,最佳代理花落谁家?
  • github.com/holiman/uint256 源码阅读
  • 排序-表排序
  • 勒索病毒最新变种.locked1勒索病毒来袭,如何恢复受感染的数据?
  • 信号补零对信号频谱的影响
  • 【Gan教程 】 什么是变分自动编码器VAE?
  • T113-S3-buildroot文件系统tar解压缩gz文件
  • 软件测试面试题:压测时,QPS一直上不去,如何排查?
  • 探索JavaScript ES6+新特性
  • Elasticsearch常见错误
  • mysql源码编译安装
  • On Moving Object Segmentation from Monocular Video with Transformers 论文阅读
  • [AutoSar NVM] 存储架构