import os
from win32com import client
def doc_to_docx(path):
if not os.path.isabs(path):
print("不是绝对路径")
return
if not os.path.exists(path):
print("文件不存在")
return
if os.path.splitext(path)[1] != ".doc":
print("文件类型不匹配")
return
# 创建 Word 应用程序对象
word_app = client.Dispatch('Word.Application')
word_app.DisplayAlerts = False
# 打开 DOC 文件
doc = word_app.Documents.Open(path)
# 将内容复制到新的 DOCX 文件中
new_docx_file = os.path.splitext(path)[0] + ".docx"
doc.SaveAs(new_docx_file, 16) # 16 表示保存为 DOCX 格式
doc.Close()
word_app.Quit()
print("文档类型转换成功。")
if __name__ == '__main__':
path = r"C:\Users\cinde\OneDrive\桌面\big.doc"
doc_to_docx(path)
python中用docx将doc文件转换为docx文件
最新推荐文章于 2024-05-10 18:45:14 发布