使用python将doc的word文件转换成docx文件

一、学习目标:

主要之前使用python提起word的docx的文件的数据。但是今天发现,如果是doc后缀的word文件,会报错,这样就无法提取数据了,然后开始搜索如果使用python将doc抓换成docx文件。发现好多文章都是使用win32com模块处理的。

二、直接转换代码:

不多说了了,直接上我整理测试成功的代码:

from win32com import client as wc #导入模块


def doc_to_docx(file):
    word = wc.Dispatch("Word.Application") # 打开word应用程序
    doc = word.Documents.Open(file) #打开word文件
    doc.SaveAs("{}x".format(file), 12)#另存为后缀为".docx"的文件,其中参数12指docx文件
    doc.Close() #关闭原来word文件
    word.Quit()
    print("完成!")
    return "{}x".format(file)


if __name__ == '__main__':
    file = r"D\sub_demand_id_653__data.doc"
    doc_to_docx(file)

记得。doc文件是绝对路径地址,不然可能报错。
如果想提取word数据,可以搜索我的另一篇博客。

参考:

https://zhuanlan.zhihu.com/p/133787171
https://zhuanlan.zhihu.com/p/64189783
更多文章搜索

猜你喜欢

转载自blog.csdn.net/weixin_42081389/article/details/108513828