如何保存从 PyUNO 编辑的文档?

发布于 2024-12-10 10:47:44 字数 480 浏览 1 评论 0原文

我现在已成功打开 Word 97-2003 (.doc) 文档并通过 Python 对其进行编辑。但我该如何保存它呢?

我总是得到:

Traceback (most recent call last):
  File "office.py", line 55, in <module>
    model.storeToUrl('file:///c:/temp/out.doc', ())
AttributeError: storeToUrl

(相关问题。)

属性应该是什么?

然后我该如何关闭该文档?

I have now succeeded in opening a Word 97-2003 (.doc) document and edited it from Python. But how do I save it?

I always get:

Traceback (most recent call last):
  File "office.py", line 55, in <module>
    model.storeToUrl('file:///c:/temp/out.doc', ())
AttributeError: storeToUrl

(Related question.)

What should the attributes be?

And how do I then close the document?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

酒废 2024-12-17 10:47:44

另一个答案网上到处都是,而且很混乱。在一些例子中,model是TEXT对象,storeToURL()和dispose()是document对象的方法,这里是另一种实现。

from com.sun.star.beans import PropertyValue
from unohelper import systemPathToFileURL

# open a writer document object
doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ())

.....

url = systemPathToFileUrl('c:/out.doc')

# NOTE THAT ARGS IS A TUPLE OF PROPERTY VALUES
args = (PropertyValue('FilterName', 0, 'MS Word 97', 0),)

doc.storeToURL(url, args)

# close the document
doc.dispose()

The other answer is all over the net, and is very confusing. In some examples, model is the TEXT object, storeToURL() and dispose() are methods of the document object, here is another implementation.

from com.sun.star.beans import PropertyValue
from unohelper import systemPathToFileURL

# open a writer document object
doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ())

.....

url = systemPathToFileUrl('c:/out.doc')

# NOTE THAT ARGS IS A TUPLE OF PROPERTY VALUES
args = (PropertyValue('FilterName', 0, 'MS Word 97', 0),)

doc.storeToURL(url, args)

# close the document
doc.dispose()
眼眸 2024-12-17 10:47:44
model.storeToURL('file:///c:/temp/out.doc', (createPropertyValue("FilterName","MS Word 97"),) 

请注意“URL”全部大写,在我的原始代码中它拼写错误,例如“Url”。

model.storeToURL('file:///c:/temp/out.doc', (createPropertyValue("FilterName","MS Word 97"),) 

Note the all caps on "URL", in my original code it was spelled wrong, like "Url".

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