Python Guide - My First Macro

From The Document Foundation Wiki
Jump to: navigation, search
This page contains changes which are not marked for translation.


Return to Index

Other languages:
English • ‎español • ‎italiano • ‎日本語 • ‎português do Brasil


My first Python Macro

In your user macros places. Any example in this guide, understands that save in this place.

IMPORTANT Python's syntax is very strict, be sure copy and paste correctly.

You used your favorite text editor, or better an IDE for edit your code.

For Writer

1 import uno
2 
3 
4 def my_first_macro_writer():
5     doc = XSCRIPTCONTEXT.getDocument()
6     text = doc.getText()  # com.sun.star.text.Text
7     text.setString('Hello World in Python in Writer')
8     return

For Calc

1 import uno
2 
3 
4 def my_first_macro_calc():
5     doc = XSCRIPTCONTEXT.getDocument()
6     cell = doc.Sheets[0]['A1']  # com.sun.star.sheet.XSpreadsheetDocument
7     cell.setString('Hello World in Python in Calc')
8     return

Execute macro

For Writer

  • Open Writer, go to Tools ▸ Macros ▸ Run macro... in section Library select mymacros (or your name file), in section Macro Name, select macro my_first_macro_writer and click in command button Run

Demo Writer

For Calc

  • Open calc, go to Tools ▸ Macros ▸ Run macro... in section Library select mymacros (or your name file), in section Macro Name, select macro my_first_macro_calc and click in command button Run

Demo Calc

Return to Index