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.
为“Excel.Application”运行相同的代码(稍作修改 - 只是为了使其工作)给了我很好的结果。但是,当我为MSWord使用doc.Save()或doc.SaveAs(same_file)时,它会默默地失败。我不知道为什么,但没有保存更改。
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?
现在我的解决方法是将SaveAs用于另一个文件,它也很好用。但我想了解为什么我对MSWord文件有如此奇怪的行为以及如何修复它?
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 :)
编辑:我改变了我的代码,而不是误导了人们因为尝试/除外的无声失败原因。但是,感谢他们所有人在我的代码中发现了这个缺陷:)
4 个解决方案
#1
3
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.
您以错误的方式使用CustomDocumentProperties,正如其他人所指出的那样,您无法看到它,因为您正在吞下异常。
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.
注意:没有错误处理,它绝对不是生产质量,但它应该足以实现您的功能。最后,我猜测属性类型的值(并且对于字符串类型猜测是正确的),但对于其他类型,可能存在一些问题。
#2
1
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.
只有在Value成功更改后才能保存文件。也许你可以尝试删除try-except子句,看看当你没有保存文件时实际发生了什么。而且,顺便说一下,使用裸露的不是一个好习惯。
#3
0
(a) Check to see if you have file write access
(a)检查您是否具有文件写入权限
(b) Make sure you code catches using the COMException
(b)确保使用COMException编写捕获代码
(C) are you gracefully terminating excel/words when creating multiple documents
(C)你在创建多个文档时优雅地终止excel / words
Darknight
#4
0
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.
保存Word文件通常失败的最常见原因是它在Word中打开。
#1
3
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.
您以错误的方式使用CustomDocumentProperties,正如其他人所指出的那样,您无法看到它,因为您正在吞下异常。
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.
注意:没有错误处理,它绝对不是生产质量,但它应该足以实现您的功能。最后,我猜测属性类型的值(并且对于字符串类型猜测是正确的),但对于其他类型,可能存在一些问题。
#2
1
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.
只有在Value成功更改后才能保存文件。也许你可以尝试删除try-except子句,看看当你没有保存文件时实际发生了什么。而且,顺便说一下,使用裸露的不是一个好习惯。
#3
0
(a) Check to see if you have file write access
(a)检查您是否具有文件写入权限
(b) Make sure you code catches using the COMException
(b)确保使用COMException编写捕获代码
(C) are you gracefully terminating excel/words when creating multiple documents
(C)你在创建多个文档时优雅地终止excel / words
Darknight
#4
0
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.
保存Word文件通常失败的最常见原因是它在Word中打开。