2025年3月27日 星期四 甲辰(龙)年 月廿六 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

python批量将文件夹内所有doc转成docx

时间:10-14来源:作者:点击数:27
CDSY,CDSY.XYZ

python批量将文件夹内所有doc转成docx

doc转docx函数

  • import os
  • from win32com import client
  • def doc_to_docx(path):
  • if os.path.splitext(path)[1] == ".doc":
  • word = client.Dispatch('Word.Application')
  • doc = word.Documents.Open(path) # 目标路径下的文件
  • doc.SaveAs(os.path.splitext(path)[0]+".docx", 16) # 转化后路径下的文件
  • doc.Close()
  • word.Quit()
  • path = ""#填写文件夹路径
  • doc_to_docx(path)

获取文件夹下的所有文件的绝对路径

  • import os
  • def find_file(path, ext, file_list=[]):
  • dir = os.listdir(path)
  • for i in dir:
  • i = os.path.join(path, i)
  • if os.path.isdir(i):
  • find_file(i, ext, file_list)
  • else:
  • if ext == os.path.splitext(i)[1]:
  • file_list.append(i)
  • return file_list
  • dir_path = ""
  • ext = ".doc"
  • file_list = find_file(dir_path, ext)

源码

  • import os
  • from win32com import client
  • def doc_to_docx(path):
  • if os.path.splitext(path)[1] == ".doc":
  • word = client.Dispatch('Word.Application')
  • doc = word.Documents.Open(path) # 目标路径下的文件
  • doc.SaveAs(os.path.splitext(path)[0]+".docx", 16) # 转化后路径下的文件
  • doc.Close()
  • word.Quit()
  • def find_file(path, ext, file_list=[]):
  • dir = os.listdir(path)
  • for i in dir:
  • i = os.path.join(path, i)
  • if os.path.isdir(i):
  • find_file(i, ext, file_list)
  • else:
  • if ext == os.path.splitext(i)[1]:
  • file_list.append(i)
  • return file_list
  • dir_path = "C:\Users\python"#批量转换文件夹
  • ext = ".doc"
  • file_list = find_file(dir_path, ext)
  • for file in file_list:
  • doc_to_docx(file)
CDSY,CDSY.XYZ
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐