使用python做了一个pdf转word的小工具

写在前面:近来工作上一直在处理word excel等文档的处理;于是利用新学的python写了一个小工具

1、其实实现起来很简单,主要是利用了pdf2docx这个库来实现

首先安装导入pdf2docx库

python 复制代码
pip install pdf2docx

2、代码部分:

话不多说直接上代码:

python 复制代码
import os
from pdf2docx import Converter
from loguru import logger
from tkinter import Tk, filedialog

# 获取到当前路径
path = os.getcwd()

# 打开文件选择对话框,选择要转换的PDF文件
def choose_pdf_file():
    root = Tk()
    root.withdraw()
    file_path = filedialog.askopenfilename(filetypes=[("PDF Files", "*.pdf")])
    root.destroy()
    return file_path

def main():
    # 选择要转换的PDF文件
    pdf_file = choose_pdf_file()

    if not pdf_file:
        logger.debug("未选择任何PDF文件。")
        return

    docx_file = os.path.splitext(pdf_file)[0] + ".docx"
    logger.debug("原始的PDF文件为-------%s" % pdf_file)

    cv = Converter(pdf_file)
    cv.convert(docx_file)
    logger.debug("转换之后的DOCX文件为-------%s" % docx_file)
    cv.close()

if __name__ == "__main__":
    main()
相关推荐
databook17 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室18 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三19 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户2519162427111 天前
Python之语言特点
python
刘立军1 天前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机1 天前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i1 天前
django中的FBV 和 CBV
python·django
c8i1 天前
python中的闭包和装饰器
python
这里有鱼汤1 天前
小白必看:QMT里的miniQMT入门教程
后端·python