OpenOffice Application with Python

download OpenOffice Application with Python

If you can't read please download the document

Transcript of OpenOffice Application with Python

Apache OpenOffice Presentation Template

OpenOffice Application with Python

[email protected]/5/17

OpenOffice Application with Python is created by imacat (Yang Shih-Ching),and licensed under a Creative Commons Attribution 3.0 Unported License.

imacat

A member of the Apache OpenOffice Project Management Committee

An OpenOffice committer

The system administrator of the OpenOffice forum and Wiki

A PyLadies Taiwan organizer

Graduated from National Taiwan Normal University

Why OpenOffice?

Why OpenOffice?

Free and open source

Why OpenOffice?

Free and open source

Cross platform Windows, Mac, Linux, BSD

Why OpenOffice?

Free and open source

Cross platform Windows, Mac, Linux, BSD

Open and standard file format ODF

Why OpenOffice?

Free and open source

Cross platform Windows, Mac, Linux, BSD

Open and standard file format ODF

Excellent API design

OpenOffice UNO API

OpenOffice UNO API

Universal Network Object

OpenOffice UNO API

Universal Network ObjectUniversal among programming languages

OpenOffice UNO API

Universal Network ObjectUniversal among programming languages

Networked API

OpenOffice Object System

OpenOffice Program

Writer

Calc

Impress

Applications

OpenOffice Object System

OpenOffice Program

Writer

Calc

Impress

Components

OpenOffice Object System

OpenOffice Desktop

Text Document

Spreadsheet Document

Presentation Document

Components

OpenOffice Object System

OpenOffice Desktop

Text Document

Spreadsheet Document

Presentation Document

Paragraphs

Text Ranges

Tables

Shapes

Text Cursors

Components

OpenOffice Object System

OpenOffice Desktop

Text Document

Spreadsheet Document

Presentation Document

Spreadsheets

Sheet Cells

Data Pilots

Database Ranges

Charts

Components

OpenOffice Object System

OpenOffice Desktop

Text Document

Spreadsheet Document

Presentation Document

Draw Pages

Shapes

Charts

Presentation

Slide Show Controller

OpenOffice UNO API

OpenOffice

Writer

Calc

Impress

UNOAPI

Language Binding

OpenOffice UNO API

OpenOffice

Writer

Calc

Impress

UNOAPI

Language Binding

OpenOffice UNO API

OpenOffice

Writer

Calc

Impress

UNOAPI

C++

Java

Python

Language Binding

OpenOffice UNO API

OpenOffice

Writer

Calc

Impress

UNOAPI

C++

Java

Perl

Python

Language Binding

OpenOffice UNO API

OpenOffice

Writer

Calc

Impress

UNOAPI

C++

Java

Perl

Python

OLE

CLI

COM

Language Binding

OpenOffice UNO API

OpenOffice

Writer

Calc

Impress

UNOAPI

C++

Java

Perl

Python

C#

VB.NET

OLE

CLI

COM

Language Binding

OpenOffice UNO API

OpenOffice

Writer

Calc

Impress

UNOAPI

C++

Java

Perl

Python

AlmostAnything!

C#

VB.NET

OLE

CLI

COM

Language Binding

OpenOffice UNO API

OpenOffice

Writer

Calc

Impress

UNOAPI

C++

Java

Perl

Python

AlmostAnything!

C#

VB.NET

OLE

CLI

COM

NetworkSocket

Client Application A

OpenOffice.org UNO

OpenOffice.org

Writer

Calc

Impress

UNOAPI

C++

Client Application A

OpenOffice.org UNO

OpenOffice.org

Writer

Calc

Impress

UNOAPI

C++

Client Application B

Java

Client Application A

OpenOffice.org UNO

OpenOffice.org

Writer

Calc

Impress

UNOAPI

C++

Client Application B

Java

Client Application C

Python

Client A

Server

Client Application A

OpenOffice.org UNO

OpenOffice.org

Writer

Calc

Impress

UNOAPI

C++

Client B

Client Application B

Java

Client C

Client Application C

Python

TCP/IPNetwork

OpenOffice UNO API

Universal Network ObjectUniversal among programming languages

Networked API

Write your office application in your favorite language!

But, which language to use?

Language Binding

Choosing Language
for UNO Applications

OpenOffice

Writer

Calc

Impress

UNOAPI

C++

Java

Perl

Python

AlmostAnything!

C#

VB.NET

OLE

CLI

COM

NetworkSocket

Language Binding

Directly Supported Languages
C++, Java and Python

OpenOffice

Writer

Calc

Impress

UNOAPI

C++

Java

Perl

Python

AlmostAnything!

C#

VB.NET

OLE

CLI

COM

NetworkSocket

Language Binding

Dynamic Typed Language
Python

OpenOffice

Writer

Calc

Impress

UNOAPI

C++

Java

Perl

Python

AlmostAnything!

C#

VB.NET

OLE

CLI

COM

NetworkSocket

Choosing PyUNO
for OpenOffice UNO Applications

Directly supported by OpenOffice itself

Choosing PyUNO
for OpenOffice UNO Applications

Directly supported by OpenOffice itself

Dynamic typing making everything easier

Connecting to OpenOffice
with Java UNO

XComponentContext localContext = Bootstrap.createInitialComponentContext(null);

XMultiComponentFactory localServiceManager = localContext.getServiceManager();

Object unoUrlResolver = localServiceManager.createInstanceWithContext(

"com.sun.star.bridge.UnoUrlResolver", localContext);

XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(

XUnoUrlResolver.class, unoUrlResolver);

Object bootstrapContext = xUnoUrlResolver.resolve(String.format(

"uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext"));

XComponentContext xBootstrapContext = (XComponentContext) UnoRuntime.queryInterface(

XComponentContext.class, bootstrapContext);

XMultiComponentFactory serviceManager = xBootstrapContext.getServiceManager();

Object desktop = this.serviceManager.createInstanceWithContext(

"com.sun.star.frame.Desktop", xBootstrapContext);

Connecting to OpenOffice
with PyUNO

local_context = uno.getComponentContext()

local_service_manager = local_context.getServiceManager()

uno_url_resolver = local_service_manager.createInstanceWithContext(

'com.sun.star.bridge.UnoUrlResolver', local_context)

bootstrap_context = uno_url_resolver.resolve(

'uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext')

service_manager = bootstrap_context.getServiceManager()

desktop = service_manager.createInstanceWithContext(

'com.sun.star.frame.Desktop', bootstrap_context)

Connecting to OpenOffice
with Java UNO vs. PyUNO

// Java UNO

XComponentContext localContext =

XMultiComponentFactory localSM =

Object unoUrlResolver =

XUnoUrlResolver xUnoUrlResolver =

Object bootstrapContext =

XComponentContext xBootstrapContext =

XMultiComponentFactory serviceManager =

Object desktop =

# PyUNO

local_context = uno.getComponentContext()

local_service_manager =

uno_url_resolver =

bootstrap_context =

service_manager =

desktop =

Choosing PyUNO
for OpenOffice UNO Applications

Directly supported by OpenOffice itself

Dynamic typing making everything easierYou don't need to obtain an object's type in order to use it

Choosing PyUNO
for OpenOffice UNO Applications

Directly supported by OpenOffice itself

Dynamic typing making everything easierYou don't need to obtain an object's type in order to use it

To read, write and maintain codes become a lot easier

OpenOffice Python Support

OpenOffice ship with a Python installation

OpenOffice Python Support

OpenOffice ship with a Python installationLinux: /opt/openoffice4/program/python

OpenOffice Python Support

OpenOffice ship with a Python installationLinux: /opt/openoffice4/program/python

Windows: "C:\Program Files (x86)\OpenOffice 4\program\python.exe"

OpenOffice Python Support

OpenOffice ship with a Python installationLinux: /opt/openoffice4/program/python

Windows: "C:\Program Files (x86)\OpenOffice 4\program\python.exe"

Version 2.7.6 as of OpenOffice 4.1

SimpleExample

Demonstration

Running OpenOffice as a Server

Connecting to OpenOffice

OpenOffice Operation

Running OpenOffice
as a Server

In Linux% /opt/openoffice4/program/soffice.bin \

"-accept=socket,host=localhost,port=2002;urp;"

In WindowsC:\>"C:\Program Files (x86)\OpenOffice 4\program\soffice.exe"

"-accept=socket,host=localhost,port=2002;urp;"

Connecting to OpenOffice
with PyUNO

% /opt/openoffice4/program/python

>>> import uno

>>> local_context = uno.getComponentContext()

>>> local_service_manager = local_context.getServiceManager()

>>> uno_url_resolver = local_service_manager.createInstanceWithContext(

... 'com.sun.star.bridge.UnoUrlResolver', local_context)

>>> bootstrap_context = uno_url_resolver.resolve(

... 'uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext')

>>> service_manager = bootstrap_context.getServiceManager()

>>> desktop = service_manager.createInstanceWithContext(

... 'com.sun.star.frame.Desktop', bootstrap_context)

>>>

Checking the desktop Object

>>> dir(desktop)

['ActiveFrame', 'ActiveTask', 'ComponentWindow', 'Components', 'ContainerWindow', 'Controller', 'Creator', 'CurrentComponent', 'CurrentFrame', ]

>>>

Checking the Current Document

>>> doc = desktop.CurrentComponent

>>> dir(doc)

['ActionLocks', 'AllowMacroExecution', 'ApplyFormDesignMode', 'AreaLinks', 'Args', 'AutomaticControlFocus', 'AvailableServiceNames', 'AvailableViewControllerNames', ]

>>>

Checking the Spreadsheets

>>> sheets = doc.Sheets

>>> dir(sheets)

['Count', 'ElementNames', 'ElementType', 'ImplementationId', 'ImplementationName', 'SupportedServiceNames', 'Types', 'copyByName', 'createEnumeration', 'getByIndex', 'getByName', ]

>>>

Checking the First Sheet

>>> sheet = sheets.getByIndex(0)

>>> dir(sheet)

['AbsoluteName', 'Annotations', 'ArrayFormula', 'ArrayTokens', 'AsianVerticalMode', 'AutomaticPrintArea', 'BorderColor', 'BottomBorder', 'CellBackColor', 'CellFormatRanges', ]

>>>

Checking A1

>>> cell = sheet.getCellRangeByName('A1')

>>> dir(cell)

['AbsoluteName', 'ActionLocks', 'Annotation', 'ArrayFormula', 'ArrayTokens', 'AsianVerticalMode', 'BottomBorder', 'CellAddress', 'CellBackColor', ]

>>>

Updating A1

>>> cell.CellBackColor = 0xFF0000

>>> cell.String = 'Hello, world!'

>>>

Updating A1, A2 and A3

>>> sheet.getCellRangeByName('A1').String = 'Betty'

>>> sheet.getCellRangeByName('A2').Value = 1630

>>> sheet.getCellRangeByName('A3').Formula = '=A2*6'

>>>

Creating a Multiplication Table

>>> for i in range(1, 10):

... for j in range(1, 10):

... sheet.getCellByPosition(i-1, j-1).Value = i*j

...

>>>

Writing Your Own UNO Applicationis Easy!

Thank you.Any questions?