Python Guide - My First Macro

From The Document Foundation Wiki
Jump to: navigation, search


Return to Index

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()
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']
7     cell.setString('Hello World in Python in Calc')
8     return

Execute macro

  • Open Writer, go to Tools->Macro->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


  • Open calc, go to Tools->Macro->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