Code: Select all
desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
Code: Select all
desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
Code: Select all
>>> from com.sun.star.beans import PropertyValue
>>> propVal = PropertyValue()
>>> propVal.Name = "URL" # or "FileName"?
>>> propVal.Value = "file:///path/to/document.odt"
>>> document = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, (propVal,))
Code: Select all
>>> document = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ())
>>> document.load( (propVal,) )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.DoubleInitializationException
Code: Select all
>>> document = desktop.loadComponentFromURL("file:///path/to/document.odt", "_blank", 0, ())
>>> document
pyuno object (com.sun.star.lang.XComponent)0x7fb7a1c89328{implementationName=SwXTextDocument, supportedServices={com.sun.star.document.OfficeDocument,com.sun.star.text.GenericTextDocument,com.sun.star.text.TextDocument}, supportedInterfaces={...}}
Do what the user interface does: open only documents having specific extensions. Note that a Writer document may be an OpenOffice Writer, Microsoft Word, etc. The extension (and sometimes the structure of the document) is used by OpenOffice to find the filter required to load the document._savage wrote:It turns out though that I can now load any kind of file type: text documents, images, tables, ... However, I'd like to restrict the file types to supported text documents only (i.e. open Writer only) and reject the rest. Is that possible?
Once the document is loaded, it provides some services and interfaces, as you can see with MRI or Xray._savage wrote:how can I query that document object once it's open? It seems that a Writer document supports only a certain set of services
Code: Select all
...
myText = myDoc.getText()
aCursor = myText.createTextCursor()
aCursor.gotoEnd(False)
' invocation of a service of a Writer document
aSection = myDoc.createInstance("com.sun.star.text.TextSection")
myText.insertTextContent(aCursor, aSection, False)
Code: Select all
' *** Basic ***
Dim dispatcher As Object
dispatcher = CreateUnoService("com.sun.star.frame.DispatchHelper")
I wanted to avoid maintaining a list of supported document types, and then having to check them before invoking office. That's redundant and error prone. Thus, I tried two different ways of solving the problem, but I get stuck with both.B Marcelly wrote:Do what the user interface does: open only documents having specific extensions. Note that a Writer document may be an OpenOffice Writer, Microsoft Word, etc. The extension (and sometimes the structure of the document) is used by OpenOffice to find the filter required to load the document.
Correct, see the code snippet in my previous post. Note that I'd like to invoke this from a Python script, and not manually through an introspection tool.B Marcelly wrote:Once the document is loaded, it provides some services and interfaces, as you can see with MRI or Xray.
You're saying: just invoke a text-service specific function and see what happens? If office had loaded anything else but a Writer document in your example, the first line had already failed, correct?B Marcelly wrote:With Python or Basic the supported services and their interfaces are directly usable, no need for invocation (contrary to Java, C++).
The available services of the object need to be invoked from the object, but you have to know how to use them. Example, inserting a text section in a Writer document:With Python, Basic, etc, you cannot invoke an interface. You can only use the interfaces provided by a service.Code: Select all
... myText = myDoc.getText() aCursor = myText.createTextCursor() aCursor.gotoEnd(False) ' invocation of a service of a Writer document aSection = myDoc.createInstance("com.sun.star.text.TextSection") myText.insertTextContent(aCursor, aSection, False)
Code: Select all
>>> document.getText()
AttributeError: getText
Are you a programmer?_savage wrote:This tells me: that's not a Writer document?
I'm a passionate code monkey.Villeroy wrote:Are you a programmer?
Monkeys can catch very well. In this case all you need to do is catching an error._savage wrote:I'm a passionate code monkey.Villeroy wrote:Are you a programmer?
Oh yes, of course, that's not what I'm asking. What I'm asking is: Is calling getText() on a document object sufficient to make sure that this is a Writer document?Villeroy wrote:Monkeys can catch very well. In this case all you need to do is catching an error.
Then you don't have a solution better than the proposed one. Which is neither redundant nor error prone, but working on existing text documents. A working solution is better than none._savage wrote:I wanted to avoid maintaining a list of supported document types, and then having to check them before invoking office. That's redundant and error prone. Thus, I tried two different ways of solving the problem, but I get stuck with both.
MRI or Xray is only used to show you what you can expect from an object. It's up to you to understand what it displays and use available properties and methods, with the help of all documentation available on the OpenOffice API._savage wrote:I'd like to invoke this from a Python script, and not manually through an introspection tool.
Here is a Python example._savage wrote:I am still puzzled as to what properties to pass to load() for an empty Writer document object.
Code: Select all
import uno
def _ctx():
return uno.getComponentContext()
def createUnoService(serviceName):
""" same as Basic CreateUnoService """
sm = uno.getComponentContext().ServiceManager
result = sm.createInstanceWithContext(serviceName, _ctx())
return result
# ----- the program starts here ------
def HelloWorldNewDoc( ):
"""Prints the string 'Hello World(in Python)' into a new Writer document"""
desktop = createUnoService("com.sun.star.frame.Desktop")
document = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ())
myText = document.getText()
myText.String = "Hello World in a new Writer document"
return None
g_exportedScripts = (HelloWorldNewDoc)
It takes no more than a few minutes to check this out for a text, spreadsheet, drawing, presentation, database and math formula. Without an object inspector (XRay, MRI) you won't get far anyway. An inspector can tell you that every document has SupportedServiceNames and an ImplementationName to describe the document type._savage wrote:Oh yes, of course, that's not what I'm asking. What I'm asking is: Is calling getText() on a document object sufficient to make sure that this is a Writer document?Villeroy wrote:Monkeys can catch very well. In this case all you need to do is catching an error.
Code: Select all
if document.supportsService('com.sun.star.text.TextDocument'):
Thanks Piet, I saw your post on the mailing list. This works perfectly!pietvo wrote:To check if document is a Writer document:
Code: Select all
if document.supportsService('com.sun.star.text.TextDocument'):