背景:想对一个文件下下的文件及子目录的word文件批量删除页眉页脚,过程中出现document不能打开doc的文件,需要先将doc文件转换为docx文件。通过Win32com解决doc转换docx后,测试一直在单个目录下,未出现其他问题。当切换到实际目录时出现:pywintypes.com_error: (-2147352567, ‘发生意外。‘, (0, ‘Kingsoft WPS‘, ‘文档保存失败。‘, ‘‘, 3011, -214746725)) 的问题。
查找资料,以为是wps的问题,或者文件频繁打开保存导致。还一度引入time库去看文件处理的时间,没有怀疑是自己的文件路径问题。最后通过https://blog.csdn.net/q7w8e9r4/article/details/133701132,还是去print了自己的文件路径,最终解决了该问题。
以下代码仅供参考,大神轻喷
【code】:
import os
import time
import shutil
import win32com.client
from docx import Document
def delete_docx_header_footer(file_path):
doc = Document(file_path)
for section in doc.sections:
i = 0
header = section.header
footer = section.footer
if header is not