为什么 MSWord 文档的保存会默默失败?
我需要更改许多文件中的一些自定义属性值。下面是一个代码示例 - 我如何对单个文件执行此操作:
import win32com.client
MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = False
doc = MSWord.Documents.Open(file)
doc.CustomDocumentProperties('Some Property').Value = 'Some New Value'
doc.Save()
doc.Close()
MSWord.Quit()
为 "Excel.Application"
运行相同的代码(稍作更改 - 只是为了使其工作)给了我很好的结果。但是,当我对 MSWord 使用 doc.Save()
或 doc.SaveAs(same_file)
时,它会默默地失败。我不知道为什么,但更改没有保存。
现在我的解决方法是使用 SaveAs
到另一个文件,它也很好用。但我想了解为什么我对 MSWord 文件有如此奇怪的行为以及如何修复它?
编辑:我更改了我的代码,不是为了误导那些因尝试/例外而无声失败的人。 然而,感谢他们所有人在我的代码中发现了这个缺陷:)
I need to change some custom properties values in many files. Here is an example of code - how I do it for a single file:
import win32com.client
MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = False
doc = MSWord.Documents.Open(file)
doc.CustomDocumentProperties('Some Property').Value = 'Some New Value'
doc.Save()
doc.Close()
MSWord.Quit()
Running the same code for "Excel.Application"
(with minor changes - just to make it work) gives me excellent result. However when I'm using doc.Save()
or doc.SaveAs(same_file)
for MSWord it silently fails. I don't know why, but changes are not saved.
Now my workaround is to use SaveAs
to a different file, it also works good. But I want to understand why I have such strange behaviour for MSWord files and how it can be fixed?
Edit: I changed my code, not to misdirect people with silent fail cause of try/except.
However, thanks to all of them for finding that defect in my code :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您以错误的方式使用 CustomDocumentProperties ,正如其他人指出的那样,您看不到它,因为您正在吞下异常。
此外 - 在这里我在文档中找不到任何内容 - 更改属性时未重置
Saved
属性,因此文件未更改。这是正确的代码:
注意:没有错误处理,并且它绝对不具有生产质量,但它应该足以让您实现您的功能。
最后,我猜测属性类型的值(对于字符串类型,猜测是正确的),但对于其他类型可能存在一些问题。
You were using the
CustomDocumentProperties
in the wrong way, and as other people pointed out, you could not see it, because you were swallowing the exception.Moreover - and here I could not find anything in the documentation - the
Saved
property was not reset while changing properties, and for this reason the file was not changed.This is the correct code:
Note: there is no error handling, and it is definitely not of production quality, but it should be enough for you to implement your functionality.
Finally, I am guessing the values of the property types (and for the string type the guess is correct) but for the others there could be some issue.
仅当
Value
成功更改时,您才会保存文件。也许您可以尝试删除try
-except
子句并查看文件未保存时实际发生的情况。而且,顺便说一句,使用裸except
并不是一个好的做法。you're saving file only if
Value
was successfully changed. May be you could try to removetry
-except
clause and see what is actually happening when you're file is not saved. And, btw, using bareexcept
is not a good practice.Darknight
Darknight
由于您忽略错误(
除了:pass
),它会默默地失败。保存 Word 文件通常失败的最常见原因是它是在 Word 中打开的。
It fails silently since you ignore errors (
except: pass
).The most common reason why saving a Word file usually fails is that it's open in Word.