Python + COM

Keyboard macros or custom scripts

Python + COM

Postby Ian Weisser » Sun Dec 12, 2010 1:43 pm

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   Expand viewCollapse view
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   Expand viewCollapse view
document.CurrentController.Frame.ContainerWindow.Visible = False




2) Tab Stops: [SOLVED]
Code: Select all   Expand viewCollapse view
# 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   Expand viewCollapse view
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   Expand viewCollapse view
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   Expand viewCollapse view
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

Last edited by Ian Weisser on Thu Dec 16, 2010 5:24 am, edited 8 times in total.
OpenOffice 3.2.1 on Ubuntu 10.10 / LibreOffice Beta on Mac OS 10.5 / OpenOffice 3.2.1 Windows XP
Ian Weisser
 
Posts: 5
Joined: Tue Nov 30, 2010 5:05 am

Re: Python + COM

Postby rudolfo » Sun Dec 12, 2010 9:36 pm

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.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1416
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Python + COM

Postby Ian Weisser » Tue Dec 14, 2010 8:47 pm

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.
OpenOffice 3.2.1 on Ubuntu 10.10 / LibreOffice Beta on Mac OS 10.5 / OpenOffice 3.2.1 Windows XP
Ian Weisser
 
Posts: 5
Joined: Tue Nov 30, 2010 5:05 am


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 6 guests