为什么 MSWord 文档的保存会默默失败?

发布于 2024-08-03 07:02:03 字数 682 浏览 3 评论 0原文

我需要更改许多文件中的一些自定义属性值。下面是一个代码示例 - 我如何对单个文件执行此操作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撞了怀 2024-08-10 07:02:03

您以错误的方式使用 CustomDocumentProperties ,正如其他人指出的那样,您看不到它,因为您正在吞下异常。

此外 - 在这里我在文档中找不到任何内容 - 更改属性时未重置 Saved 属性,因此文件未更改。

这是正确的代码:

msoPropertyTypeBoolean = 0
msoPropertyTypeDate = 1
msoPropertyTypeFloat = 2
msoPropertyTypeNumber = 3
msoPropertyTypeString = 4

import win32com.client

MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = False

doc = MSWord.Documents.Open(file)
csp = doc.CustomDocumentProperties
csp.Add('Some Property', False, msoPropertyTypeString, 'Some New Value')
doc.Saved = False
doc.Save()
doc.Close()

MSWord.Quit()

注意:没有错误处理,并且它绝对不具有生产质量,但它应该足以让您实现您的功能。
最后,我猜测属性类型的值(对于字符串类型,猜测是正确的),但对于其他类型可能存在一些问题。

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:

msoPropertyTypeBoolean = 0
msoPropertyTypeDate = 1
msoPropertyTypeFloat = 2
msoPropertyTypeNumber = 3
msoPropertyTypeString = 4

import win32com.client

MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = False

doc = MSWord.Documents.Open(file)
csp = doc.CustomDocumentProperties
csp.Add('Some Property', False, msoPropertyTypeString, 'Some New Value')
doc.Saved = False
doc.Save()
doc.Close()

MSWord.Quit()

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.

Oo萌小芽oO 2024-08-10 07:02:03

仅当 Value 成功更改时,您才会保存文件。也许您可以尝试删除 try-except 子句并查看文件未保存时实际发生的情况。而且,顺便说一句,使用裸 except 并不是一个好的做法。

you're saving file only if Value was successfully changed. May be you could try to remove try-except clause and see what is actually happening when you're file is not saved. And, btw, using bare except is not a good practice.

夜巴黎 2024-08-10 07:02:03

(a) 检查您是否具有文件写入权限

(b) 确保使用 COMException 进行代码捕获

(C) 创建多个文档时是否优雅地终止 excel/words

Darknight

(a) Check to see if you have file write access

(b) Make sure you code catches using the COMException

(C) are you gracefully terminating excel/words when creating multiple documents

Darknight

坏尐絯℡ 2024-08-10 07:02:03

由于您忽略错误(除了: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文