[Solved] Add Math object to Writer by Python code

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
barak1969
Posts: 4
Joined: Sat Jul 28, 2018 9:15 pm

[Solved] Add Math object to Writer by Python code

Post by barak1969 »

how to write a code/macro in python to add math formula object?
Last edited by barak1969 on Sun Jul 29, 2018 11:18 am, edited 1 time in total.
openoffice 4.1.5 on windows10
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: add math object to writer by python code

Post by FJCC »

This Basic code from an old post works for me.

Code: Select all

Sub InsertEquationAtViewCursor'(oDoc, sFormula$)
  Dim oVC
  Dim oObj
  oDoc = ThisComponent
  sFormula = "x over y"
  oVC = oDoc.CurrentController.getViewCursor()
  oVC.gotoRange(oVC.getEnd(), False)
  oObj = oDoc.CreateInstance("com.sun.star.text.TextEmbeddedObject")
  oObj.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
  oObj.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
  oVC.Text.insertTextContent(oVC, oObj, False)
  oObj.EmbeddedObject.Component.formula = sFormula
  End Sub
Python code should be very similar.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
barak1969
Posts: 4
Joined: Sat Jul 28, 2018 9:15 pm

Re: add math object to writer by python code

Post by barak1969 »

thanks but the line:
oObj.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
is not recognizing com
openoffice 4.1.5 on windows10
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: add math object to writer by python code

Post by FJCC »

I think you have to import the AS_CHARACTER

Code: Select all

from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
I can't find a code example at the moment and I have to go. Try a search and I will be back later.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Add Math object to Writer by Python code

Post by FJCC »

Looking at some old code, you should put

Code: Select all

from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
at the top of your code and set

Code: Select all

 oObj.AnchorType = AS_CHARACTER
farther down in the code.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
barak1969
Posts: 4
Joined: Sat Jul 28, 2018 9:15 pm

Re: Add Math object to Writer by Python code

Post by barak1969 »

it works like magic. thanks
openoffice 4.1.5 on windows10
Post Reply