Use python to convert doc word files into docx files

1. Learning objectives:

Mainly use python to mention the data of the docx file of word. But today I found that if it is a word file with a doc suffix, it will report an error, so that the data cannot be extracted, and then start searching if you use python to grab the doc and replace it with a docx file. I found that many articles are processed using the win32com module.

2. Direct conversion code:

Not much to say, just go straight to the code I compiled and tested successfully:

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)

remember. The doc file is an absolute path address, otherwise an error may be reported.
If you want to extract word data, you can search my other blog.

reference:

https://zhuanlan.zhihu.com/p/133787171
https://zhuanlan.zhihu.com/p/64189783
More articles search

Guess you like

Origin blog.csdn.net/weixin_42081389/article/details/108513828