1 Star 0 Fork 2

w4sevens / python-word-process

forked from ypftest / python-word-process 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
python-doc-to-docx.py 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
杨鹏飞 提交于 2020-08-21 17:41 . 分步处理更新
"""
修改文档格式.doc转为.docx,并保存在子目录docx下
"""
import pythoncom
import os
import win32com.client as wc
# 从最后开始替换某字符串几次
def rreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)
# 读取文件夹下的doc文件名列表
def doc_file_name(file_dir):
fileList = []
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.doc':
fileList.append(os.path.join(root, file))
# 若.doc文档所在目录不存在docx子目录则自动创建
docx_dir = root + '\\docx'
if not os.path.exists(docx_dir):
os.makedirs(docx_dir)
return fileList
# doc文件另存为docx
def doc_to_docx(doc_name):
pythoncom.CoInitialize()
try:
word = wc.Dispatch("Word.Application")
doc = word.Documents.Open(doc_name, Encoding='utf-8')
# 上面的地方只能使用完整绝对地址,相对地址找不到文件,且,只能用“\\”,不能用“/”,哪怕加了 r 也不行,涉及到将反斜杠看成转义字符。
doc_name = rreplace(doc_name, "\\", "\\docx\\", 1)
doc.SaveAs(doc_name.replace(".doc", ".docx"), 12, False, "", True, "", False, False, False, False)
# 转换后的文件,12代表转换后为docx文件
doc.Close
except Exception as e:
print(e.message)
finally:
# 对com操作,一定要确保退出word应用
if word:
word.Quit
del word
# 释放资源
pythoncom.CoUninitialize()
def main():
file_list = doc_file_name("D:\\文件处理\\2020.8.12-350篇足球")
print(len(file_list))
for file in file_list:
doc_to_docx(file)
if __name__ == '__main__':
main()
Python
1
https://gitee.com/w4dll/python-word-process.git
git@gitee.com:w4dll/python-word-process.git
w4dll
python-word-process
python-word-process
master

搜索帮助