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

python pdf转word或excel

python pdf转word或excel

直接上源码

main

import gradio as gr
import pdf2docx as p2d
import Pdf2Excel as p2e
import utils.id.IdUtil as idUtildef convert_pdf_to(pdf_file, pdf_pwd, pdf_to_type):if pdf_to_type == "docx":# Convert PDF to DOCXcv = p2d.Converter(pdf_file, password=pdf_pwd)tmp_file = f"./output_{idUtil.IdUtil.getUUid()}.docx"cv.convert(tmp_file)cv.close()return tmp_fileelse:# Convert PDF to Excelcv = p2e.Converter(pdf_file, password=pdf_pwd)tmp_file = f"./output_{idUtil.IdUtil.getUUid()}.xlsx"cv.convert(tmp_file)return tmp_fileif __name__ == '__main__':with gr.Blocks() as b:gr.Markdown("PDF to ")with gr.Row():with gr.Column():pdf_file = gr.File(label="PDF文件", file_types=[".pdf"], file_count="single")pdf_pwd = gr.Text(label="密码,非必填")pdf_to_type = gr.Radio(["docx", "excel"], label="输出格式", value="docx")convert_btn = gr.Button("转换")out_file = gr.File(label="输出文件")convert_btn.click(convert_pdf_to, inputs=[pdf_file, pdf_pwd, pdf_to_type], outputs=[out_file],trigger_mode="once")b.launch(share=True)

Pdf2Excel

import tabula
import pandas as pdclass Converter:def __init__(self, pdf_file: str, password: str = None,):self.pdf_file = pdf_fileself.password = passworddef convert(self, out_file: str):excel_file = out_file if out_file.lower().endswith(".xlsx") else f"{out_file}.xlsx"# 读取PDF文件中的所有表格tables = tabula.read_pdf(self.pdf_file, password=self.password, pages='all', multiple_tables=True)# 创建一个Excel写入器writer = pd.ExcelWriter(excel_file)# 将每个表格合并到一个数据框中merged_table = pd.concat(tables, ignore_index=True)# 将合并的表格写入Excel文件中的一个工作表merged_table.to_excel(writer, sheet_name='All Tables', index=False)# 保存Excel文件writer.close()return f"./{excel_file}"

IdUtil

import time
import uuidclass IdUtil:# 获取一个UUid@staticmethoddef getUUid(only: bool = False, notHandler: bool = False) -> str:id = str(uuid.uuid1()) if only else str(uuid.uuid4()) + str(time.time()).split(".")[0]if notHandler:return idelse:return "".join(id.split("-"))
http://www.lryc.cn/news/444800.html

相关文章:

  • GNU链接器(LD):位置计数器(.)功能及实例解析
  • 学习记录:js算法(四十三):翻转二叉树
  • 关于 SQL 的 JOIN 操作
  • 聊聊AUTOSAR:基于Vector MICROSAR的TC8测试开发方案
  • ES6中迭代器与生成器知识浅析
  • unix中的vfork函数
  • Android 用线程池实现一个简单的任务队列(Kotlin)
  • 遨游信息技术的浩瀚宇宙:探索MySQL的深邃奥秘
  • 【Bug解决】Nacos启动成功,但却无法访问(提示:无法访问此网站,192.168.10.88的响应时间过长)
  • 【AI创作组】工程方向的硕士研究生学习Matlab的路径
  • Mac使用Nginx设置代理,并禁用自带Apache
  • AlmaLinux 安裝JDK8
  • Set 和 Map 的模拟实现
  • 深度学习自编码器 - 预测稀疏分解(PSD)篇
  • 如何检测出来这个ip是共享ip不安全
  • TMStarget学习——T1 Segmentation数据处理及解bug
  • 锁策略, cas 和 synchronized 优化过程
  • 【HTML5】html5开篇基础(2)
  • 大数据新视界 --大数据大厂之 Reactjs 在大数据应用开发中的优势与实践
  • 【论文阅读笔记】TOOD: Task-aligned One-stage Object Detection
  • 类中的特殊内容
  • network request to https://registry.npmjs.org/xxx failed, reason: connect ETIM
  • MQ入门(二):java客户端SpringAMQP
  • 软技能与AI技术的融合
  • 在视频上绘制区域:使用Vue和JavaScript实现交互式画布
  • 31. RabbitMQ顺序消费
  • BERT-BiLSTM-CRF模型实战
  • npm 安装 与 切换 淘宝镜像
  • 在Windows系统上安装的 Arrow C++ 库
  • 格雷母线电缆头安装方法视频-武汉正向科技