Python處理docx文件需要先安裝python_docx模塊
pip install python_docx
注意:不是 pip install docx
"""
修改文檔格式.docx轉為.doc,并保存到當前目錄的doc目錄下,需要提前創(chuàng)建doc目錄
"""
import pythoncom
import os
from docx import Document
# 從最后開始替換某字符串幾次
def rreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)
# 讀取文件夾下的docx文件名列表
def docx_file_name(file_dir):
fileList = []
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.docx':
fileList.append(os.path.join(root, file))
return fileList
# docx文件另存為doc
def docx_to_doc(docxName):
pythoncom.CoInitialize()
try:
doc = Document(docxName)
docxName = rreplace(docxName, "\\", "\\doc\\", 1)
doc.save(docxName.replace(".docx", ".doc"))
except Exception as e:
print(e.message)
finally:
# 釋放資源
pythoncom.CoUninitialize()
def main():
fileList = docx_file_name("D:\\文件處理\\2020.6.24文章-docx")
print(len(fileList))
for file in fileList:
docx_to_doc(file)
if __name__ == '__main__':
main()