# -*- coding: utf-8 -*-
# @Time : 2023/2/14 15:26
# @Author : ctt
# @File : transform_doc_to_docx
# @Project : 解析docx
import os
import pythoncom
import win32com.client


def closesoft():
    print('''挂载程序关闭中……
          ''')
    wc = win32com.client.constants

    try:
        wps = win32com.client.gencache.EnsureDispatch('kwps.application')
    except:
        wps = win32com.client.gencache.EnsureDispatch('wps.application')
    else:
        wps = win32com.client.gencache.EnsureDispatch('word.application')
    try:
        wps.Documents.Close()
        wps.Documents.Close(wc.wdDoNotSaveChanges)
        wps.Quit
    except:
        pass


def doc2docx(path):
    pythoncom.CoInitialize()
    w = win32com.client.Dispatch('Word.Application')
    w.Visible = 0
    w.DisplayAlerts = 0
    doc = w.Documents.Open(path)
    newpath = os.path.splitext(path)[0] + '.docx'
    doc.SaveAs(newpath, 12, False, "", True, "", False, False, False, False)
    doc.Close()
    w.Quit()
    return newpath


if __name__ == '__main__':
    closesoft()
    doc2docx(r'D:/四川报告/相关代码/从word中提取指定表格/data/四川省化工作质量安全检测研究院2022年度部门决算分析报告(1).doc')
