Page 1 of 1

Python + COM

Posted: Sun Dec 12, 2010 1:43 pm
by Ian Weisser
I'm looking at creating Writer documents using Python and the OLE/COM bridge.
I can already do this using pyuno (the 'import uno' statement that loads the uno python module) - I want to see the differences.

The really cool thing about using COM instead of pyuno is that I can use any version of Python I wish...including Python3. It breaks the annoying logjam of matching environment variables and versions and cross-connecting IDEs to work effectively - as long as the COM bridge is stable, which it seems to be.

1) Connecting via COM: How can I do it headless? I know I'm really close, but the document keeps coming up visible.

Code: Select all

import win32.client
OO_ServiceManager = win32com.client.Dispatch("com.sun.star.ServiceManager")
desktop = OO_ServiceManager.CreateInstance("com.sun.star.frame.Desktop")
OO_ServiceManager._FlagAsMethod("Bridge_GetStruct")
hidden = OO_ServiceManager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
hidden.Name = "Hidden"
hidden.Value = 1
loading_properties = tuple(hidden)
document = desktop.loadComponentFromUrl("private:factory/swriter", "_blank", 0, loading_properties)
This code works great if I want a fresh document on my desktop. How can I get OO to open headless through COM (without dropping to the command line)?
I can already simply *hide* the window, but it's slow - I want to avoid all the uneccesary display overhead.

Code: Select all

document.CurrentController.Frame.ContainerWindow.Visible = False


2) Tab Stops: [SOLVED]

Code: Select all

# Create Tab Stops
if using_uno:
    tab_stop1 = uno.createUnoStruct("com.sun.star.style.TabStop")
elif using_com:
    OO_Service_Manager._FlagAsMethod("Bridge_GetStruct")
    tab_stop1 = OO_Service_Manager.Bridge_GetStruct("com.sun.star.style.TabStop")
tab_stop1.Position = 2550                       # Both pyuno and COM
tab_stop1.Alignment = 0                         # Both pyuno and COM


3) Italic text: [SOLVED]

Code: Select all

if using_uno:
    cursor.CharPosture = "Italic"
    cursor.CharPosture = "None"
elif using_com:
    cursor.setPropertyValue("CharPosture", 1)   # Italic
    cursor.setPropertyValue("CharPosture", 0)   # Normal


4) Paragraph Breaks: [SOLVED]

Code: Select all

if using_uno:
    from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
    document.text.insertControlCharacter(cursor, PARAGRAPH_BREAK, 0)
elif using_com:
    document.text.insertControlCharacter(cursor, 0 , False)   # 0 = Paragraph break


5) Page Breaks:[SOLVED]

Code: Select all

if using_uno:
    from com.sun.star.style.BreakType import PAGE_AFTER 
    cursor.BreakType = "PAGE_AFTER"
elif using_com:
    cursor.setPropertyValue("BreakType", 5)  # 5 = PAGE_AFTER

Re: Python + COM

Posted: Sun Dec 12, 2010 9:36 pm
by rudolfo
Ian Weisser wrote:I'm looking at creating Writer documents using Python and the OLE/COM bridge.
I can already do this using pyuno - I want to see the differences.
What kind of differences do you want to see? Regarding performance I would say (without having tested this) that the pyuno method is faster. Simply because it is only the python-UNO bindings that are used, while for OLE you have the the UNO-COM bridge plus the COM binding in the python module.
And of course, COM is not cross-platform.

OLE/COM might be simpler, or at least easier to start for users with a lot of MS Windows experience. But at the end it comes all down on understanding the UNO objects, if you want to work effeciently with OpenOfffice.

Re: Python + COM

Posted: Tue Dec 14, 2010 8:47 pm
by Ian Weisser
I mostly want to see the differences in how to use the API via python-win32client (COM) vs python-pyuno. dir() and other introspection tools for pyuno don't work with the COM Bridge, of course, so it's easy to stuck.

I also want to see if Python3 via COM is a viable alternative for my work to Python26 via pyuno.

EDIT: It is! Python3 via COM (instead of pyuno) is fabulous and reliable. I was able to finally remove my old Python26 install and convert completely to P3.
The instropsection tools are not very useful, and the OLE/COM documentation is awful, but on most hiccups the UNO API is more closely followed in the COM Bridge than in the Pyuno Bridge.