三步免费PDF转WORD

第一步:安装包

pip install pdf2docx

第二步:敲代码

# -*- coding: utf-8 -*-
"""
Created on Mon Mar 18 21:51:30 2024
把当前目录下所有PDF文件转换成WORD,并保存到当前目录下的\DOC目录中
@author: hjf
"""
import os
import tkinter as tk
from tkinter import filedialog
from pdf2docx import Converter

def pdf_to_word(pdf_file, word_file):
   cv = Converter(pdf_file)
   cv.convert(word_file,start=0,end=None)
   cv.close()
   
def traverse_pdf_file_dir(directory):
    os.walk(directory)
    if not os.path.exists("doc"):#准备存放目录,如果不存在就创建它
        print("目录 doc 不存在。创建doc 目录")
        os.mkdir("doc")
    else:
        print("目录 doc 已存在。无需创建")
    
    for root, dirs, files in os.walk(directory):
        for file in files:
            # 检查文件扩展名是否为'.pdf'
            if file.endswith('.pdf'):
                file_path = os.path.join(root, file)
                # 准备存放文件名               
                excel_file = os.path.dirname(file_path)
                excel_file = excel_file + "\doc\\"+os.path.basename(file_path)
                excel_file = os.path.splitext(excel_file)#分割文件主名和扩展名
                excel_file = excel_file[0]+".docx"  #列表第一个元素存放主名
                print(file_path)
                print(excel_file)
                # 调用函数将 PDF 文件转换为 word 文件
                # 将转换后的word文件放到‘doc’目录下
                pdf_to_word(file_path, excel_file)
                
# 指定要遍历的目录路径
directory_path = os.getcwd()
traverse_pdf_file_dir(directory_path)


第三步:运行

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值