OpenOffice Application with Python

1,568 views

Published on

PyCon APAC 2014
https://tw.pycon.org/2014apac/zh/program/
2014/5/17 15:00
中研院人社中心國際會議聽
International Conference Hall, Humanities and Social Science Building, Academia Sinica, Taipei, Taiwan

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,568
On SlideShare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
Downloads
33
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

OpenOffice Application with Python

  1. 1. OpenOffice Application with Python imacat imacat@mail.imacat.idv.tw 2014/5/17
  2. 2. “OpenOffice Application with Python” is created by imacat (Yang Shih-Ching), and licensed under a Creative Commons Attribution 3.0 Unported License.
  3. 3. 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
  4. 4. Why OpenOffice?
  5. 5. Why OpenOffice? ● Free and open source
  6. 6. Why OpenOffice? ● Free and open source ● Cross platform – Windows, Mac, Linux, BSD
  7. 7. Why OpenOffice? ● Free and open source ● Cross platform – Windows, Mac, Linux, BSD ● Open and standard file format – ODF
  8. 8. Why OpenOffice? ● Free and open source ● Cross platform – Windows, Mac, Linux, BSD ● Open and standard file format – ODF ● Excellent API design
  9. 9. OpenOffice UNO API
  10. 10. OpenOffice UNO API ● Universal Network Object
  11. 11. OpenOffice UNO API ● Universal Network Object – Universal among programming languages
  12. 12. OpenOffice UNO API ● Universal Network Object – Universal among programming languages – Networked API
  13. 13. OpenOffice Object System OpenOffice ProgramOpenOffice Program WriterWriter CalcCalc ImpressImpress
  14. 14. ApplicationsApplications OpenOffice Object System OpenOffice ProgramOpenOffice Program WriterWriter CalcCalc ImpressImpress
  15. 15. ComponentsComponents OpenOffice Object System OpenOffice DesktopOpenOffice Desktop Text Document Text Document Spreadsheet Document Spreadsheet Document Presentation Document Presentation Document
  16. 16. ComponentsComponents OpenOffice Object System OpenOffice DesktopOpenOffice Desktop Text Document Text Document Spreadsheet Document Spreadsheet Document Presentation Document Presentation Document ParagraphsParagraphs Text RangesText Ranges TablesTables ShapesShapes …… Text CursorsText Cursors
  17. 17. ComponentsComponents OpenOffice Object System OpenOffice DesktopOpenOffice Desktop Text Document Text Document Spreadsheet Document Spreadsheet Document Presentation Document Presentation Document SpreadsheetsSpreadsheets Sheet CellsSheet Cells ……Data PilotsData Pilots Database RangesDatabase Ranges ChartsCharts
  18. 18. ComponentsComponents OpenOffice Object System OpenOffice DesktopOpenOffice Desktop Text Document Text Document Spreadsheet Document Spreadsheet Document Presentation Document Presentation Document Draw PagesDraw Pages ShapesShapes …… ChartsChartsPresentationPresentation Slide Show ControllerSlide Show Controller
  19. 19. OpenOffice UNO API OpenOfficeOpenOffice WriterWriter CalcCalc Impres s Impres s UNO API UNO API ……
  20. 20. Language BindingLanguage Binding OpenOffice UNO API OpenOfficeOpenOffice WriterWriter CalcCalc Impres s Impres s UNO API UNO API ……
  21. 21. Language BindingLanguage Binding OpenOffice UNO API OpenOfficeOpenOffice WriterWriter CalcCalc Impres s Impres s UNO API UNO API C++C++ JavaJava …… PythonPython
  22. 22. Language BindingLanguage Binding OpenOffice UNO API OpenOfficeOpenOffice WriterWriter CalcCalc Impres s Impres s UNO API UNO API C++C++ JavaJava PerlPerl PythonPython ……
  23. 23. Language BindingLanguage Binding OpenOffice UNO API OpenOfficeOpenOffice WriterWriter CalcCalc Impres s Impres s UNO API UNO API C++C++ JavaJava PerlPerl PythonPython OLEOLE CLICLI COMCOM……
  24. 24. Language BindingLanguage Binding OpenOffice UNO API OpenOfficeOpenOffice WriterWriter CalcCalc Impres s Impres s UNO API UNO API C++C++ JavaJava PerlPerl PythonPython …… C#C# VB.NETVB.NET OLEOLE CLICLI COMCOM……
  25. 25. Language BindingLanguage Binding OpenOffice UNO API OpenOfficeOpenOffice WriterWriter CalcCalc Impres s Impres s UNO API UNO API C++C++ JavaJava PerlPerl PythonPython Almost Anything! Almost Anything! …… C#C# VB.NETVB.NET OLEOLE CLICLI COMCOM……
  26. 26. Language BindingLanguage Binding OpenOffice UNO API OpenOfficeOpenOffice WriterWriter CalcCalc Impres s Impres s UNO API UNO API C++C++ JavaJava PerlPerl PythonPython Almost Anything! Almost Anything! …… C#C# VB.NETVB.NET OLEOLE CLICLI COMCOM…… Network Socket Network Socket
  27. 27. Client Application AClient Application A OpenOffice.org UNO OpenOffice.orgOpenOffice.org WriterWriter CalcCalc Impres s Impres s UNO API UNO API …… C++C++
  28. 28. Client Application AClient Application A OpenOffice.org UNO OpenOffice.orgOpenOffice.org WriterWriter CalcCalc Impres s Impres s UNO API UNO API …… C++C++ Client Application BClient Application B JavaJava
  29. 29. Client Application AClient Application A OpenOffice.org UNO OpenOffice.orgOpenOffice.org WriterWriter CalcCalc Impres s Impres s UNO API UNO API …… C++C++ Client Application BClient Application B JavaJava Client Application CClient Application C PythonPython
  30. 30. Client AClient A ServerServer Client Application AClient Application A OpenOffice.org UNO OpenOffice.orgOpenOffice.org WriterWriter CalcCalc Impres s Impres s UNO API UNO API …… C++C++ Client BClient B Client Application BClient Application B JavaJava Client CClient C Client Application CClient Application C PythonPython TCP/IP Network TCP/IP Network
  31. 31. OpenOffice UNO API ● Universal Network Object – Universal among programming languages – Networked API ● Write your office application in your favorite language!
  32. 32. But, which language to use?
  33. 33. Language BindingLanguage Binding Choosing Language for UNO Applications OpenOfficeOpenOffice WriterWriter CalcCalc Impres s Impres s UNO API UNO API C++C++ JavaJava PerlPerl PythonPython Almost Anything! Almost Anything! …… C#C# VB.NETVB.NET OLEOLE CLICLI COMCOM…… Network Socket Network Socket
  34. 34. Language BindingLanguage Binding Directly Supported Languages C++, Java and Python OpenOfficeOpenOffice WriterWriter CalcCalc Impres s Impres s UNO API UNO API C++C++ JavaJava PerlPerl PythonPython Almost Anything! Almost Anything! …… C#C# VB.NETVB.NET OLEOLE CLICLI COMCOM…… Network Socket Network Socket
  35. 35. Language BindingLanguage Binding Dynamic Typed Language Python OpenOfficeOpenOffice WriterWriter CalcCalc Impres s Impres s UNO API UNO API C++C++ JavaJava PerlPerl PythonPython Almost Anything! Almost Anything! …… C#C# VB.NETVB.NET OLEOLE CLICLI COMCOM…… Network Socket Network Socket
  36. 36. Choosing PyUNO for OpenOffice UNO Applications ● Directly supported by OpenOffice itself
  37. 37. Choosing PyUNO for OpenOffice UNO Applications ● Directly supported by OpenOffice itself ● Dynamic typing making everything easier
  38. 38. 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);
  39. 39. 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)
  40. 40. 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 = …
  41. 41. Choosing PyUNO for OpenOffice UNO Applications ● Directly supported by OpenOffice itself ● Dynamic typing making everything easier – You don't need to obtain an object's type in order to use it
  42. 42. Choosing PyUNO for OpenOffice UNO Applications ● Directly supported by OpenOffice itself ● Dynamic typing making everything easier – You don't need to obtain an object's type in order to use it – To read, write and maintain codes become a lot easier
  43. 43. OpenOffice Python Support ● OpenOffice ship with a Python installation
  44. 44. OpenOffice Python Support ● OpenOffice ship with a Python installation – Linux: /opt/openoffice4/program/python
  45. 45. OpenOffice Python Support ● OpenOffice ship with a Python installation – Linux: /opt/openoffice4/program/python – Windows: "C:Program Files (x86)OpenOffice 4programpython.exe"
  46. 46. OpenOffice Python Support ● OpenOffice ship with a Python installation – Linux: /opt/openoffice4/program/python – Windows: "C:Program Files (x86)OpenOffice 4programpython.exe" – Version 2.7.6 as of OpenOffice 4.1
  47. 47. Simple Example
  48. 48. Demonstration ● Running OpenOffice as a Server ● Connecting to OpenOffice ● OpenOffice Operation
  49. 49. Running OpenOffice as a Server ● In Linux – % /opt/openoffice4/program/soffice.bin – "-accept=socket,host=localhost,port=2002;urp;" ● In Windows – C:>"C:Program Files (x86)OpenOffice 4programsoffice.exe" – "-accept=socket,host=localhost,port=2002;urp;"
  50. 50. 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) ● >>>
  51. 51. Checking the desktop Object ● >>> dir(desktop) ● ['ActiveFrame', 'ActiveTask', 'ComponentWindow', 'Components', 'ContainerWindow', 'Controller', 'Creator', 'CurrentComponent', 'CurrentFrame', …] ● >>>
  52. 52. Checking the Current Document ● >>> doc = desktop.CurrentComponent ● >>> dir(doc) ● ['ActionLocks', 'AllowMacroExecution', 'ApplyFormDesignMode', 'AreaLinks', 'Args', 'AutomaticControlFocus', 'AvailableServiceNames', 'AvailableViewControllerNames', …] ● >>>
  53. 53. Checking the Spreadsheets ● >>> sheets = doc.Sheets ● >>> dir(sheets) ● ['Count', 'ElementNames', 'ElementType', 'ImplementationId', 'ImplementationName', 'SupportedServiceNames', 'Types', 'copyByName', 'createEnumeration', 'getByIndex', 'getByName', …] ● >>>
  54. 54. Checking the First Sheet ● >>> sheet = sheets.getByIndex(0) ● >>> dir(sheet) ● ['AbsoluteName', 'Annotations', 'ArrayFormula', 'ArrayTokens', 'AsianVerticalMode', 'AutomaticPrintArea', 'BorderColor', 'BottomBorder', 'CellBackColor', 'CellFormatRanges', …] ● >>>
  55. 55. Checking A1 ● >>> cell = sheet.getCellRangeByName('A1') ● >>> dir(cell) ● ['AbsoluteName', 'ActionLocks', 'Annotation', 'ArrayFormula', 'ArrayTokens', 'AsianVerticalMode', 'BottomBorder', 'CellAddress', 'CellBackColor', …] ● >>>
  56. 56. Updating A1 ● >>> cell.CellBackColor = 0xFF0000 ● >>> cell.String = 'Hello, world!' ● >>>
  57. 57. Updating A1, A2 and A3 ● >>> sheet.getCellRangeByName('A1').String = 'Betty' ● >>> sheet.getCellRangeByName('A2').Value = 1630 ● >>> sheet.getCellRangeByName('A3').Formula = '=A2*6' ● >>>
  58. 58. 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 ● ... ● >>>
  59. 59. Writing Your Own UNO Application is Easy!
  60. 60. Thank you. Any questions?

×