|

楼主 |
发表于 2021-11-30 12:35:15
|
显示全部楼层
我想转化成docx再改名,为啥我转换全失败了呢
- import docx #命令行中输如pip install python-docx安装该库
- from docx import Document
- from win32com import client as wc
- import os
- path1 = r'C:\Users\Administrator\Desktop\bat\doc标题文件名'
- path2 = r'C:\Users\Administrator\Desktop\1130'
- def doc_to_docx(file):
- word_doc = word_app.Documents.Open(file) # 打开doc后缀名的文件
- # word_doc.SaveAs(docxUrl + "/" + fileName.split("/")[-1].split(".")[0] + ".docx", 12)
- word_doc.SaveAs("{}x".format(file), 12) # 另存为后缀为".docx"的文件,其中参数12或16指docx文件
- word_doc.Close() # 关闭原来的doc后缀名文件
- # word_app.Quit()
- word_app = wc.Dispatch("Word.Application")
- for path,dirs,files in os.walk(path2):
- for file in files:
- print(file)
- if file.split(".")[-1]=="doc":
- try:
- #只处理docx文件
- doc_to_docx(file)
- print(print(file + "转化成功"))
- except:
- print(file + "转化失败")
- #
- #
- # # print(os.path.join(path1, file))
- # doc = textract.process(os.path.join(path1, file)) #打开docx文件
- # title=doc.paragraphs[0].text #获取第一段内容(也就是第一行,word中回车分隔的就是不同段落)
- # os.rename(os.path.join(path1, file),os.path.join(path1,title+".docx")) #重命名
复制代码 |
|