Page 1 of 1

[Solved] Add Math object to Writer by Python code

Posted: Sat Jul 28, 2018 9:17 pm
by barak1969
how to write a code/macro in python to add math formula object?

Re: add math object to writer by python code

Posted: Sat Jul 28, 2018 9:36 pm
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.

Re: add math object to writer by python code

Posted: Sat Jul 28, 2018 9:57 pm
by barak1969
thanks but the line:
oObj.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
is not recognizing com

Re: add math object to writer by python code

Posted: Sat Jul 28, 2018 10:07 pm
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.

Re: Add Math object to Writer by Python code

Posted: Sun Jul 29, 2018 6:39 am
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.

Re: Add Math object to Writer by Python code

Posted: Sun Jul 29, 2018 11:15 am
by barak1969
it works like magic. thanks