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) # 目标路径下的文件
print("原始数据路径:"+os.path.splitext(path)[0])
doc.SaveAs(os.path.splitext(path)[0]+".docx",16) # 转化后路径下的文件
try:
print('保存文件')
doc.Close()
print('关闭Word.Application')
word.Quit()
except Exception as e:
print(e)
# 定义了一个get_file_names函数,它接受一个路径作为参数,并返回该路径下的文件名列表
def get_file_names(path):
"""
获取指定路径下的文件名列表
"""
file_names = []
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
file_names.append(file)
return file_names
# 指定路径
path = 'H:\\个人文件\\2024.1\\'
# 获取路径下的文件名列表
file_names = get_file_names(path)
# 打印文件名列表,了解目录文件,并且将doc文件转docx文件
for file_name in file_names:
print(path + file_name)
doc_to_docx(path+file_name)