[Solved] Add Math object to Writer by Python code

Keyboard macros or custom scripts

[Solved] Add Math object to Writer by Python code

Postby barak1969 » Sat Jul 28, 2018 9:17 pm

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
barak1969
 
Posts: 4
Joined: Sat Jul 28, 2018 9:15 pm

Re: add math object to writer by python code

Postby FJCC » Sat Jul 28, 2018 9:36 pm

This Basic code from an old post works for me.
Code: Select all   Expand viewCollapse view
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.
Windows 10 and Linux Mint, since 2017
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: 7056
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: add math object to writer by python code

Postby barak1969 » Sat Jul 28, 2018 9:57 pm

thanks but the line:
oObj.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
is not recognizing com
openoffice 4.1.5 on windows10
barak1969
 
Posts: 4
Joined: Sat Jul 28, 2018 9:15 pm

Re: add math object to writer by python code

Postby FJCC » Sat Jul 28, 2018 10:07 pm

I think you have to import the AS_CHARACTER
Code: Select all   Expand viewCollapse view
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.
Windows 10 and Linux Mint, since 2017
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: 7056
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Add Math object to Writer by Python code

Postby FJCC » Sun Jul 29, 2018 6:39 am

Looking at some old code, you should put
Code: Select all   Expand viewCollapse view
from com.sun.star.text.TextContentAnchorType import AS_CHARACTER

at the top of your code and set
Code: Select all   Expand viewCollapse view
oObj.AnchorType = AS_CHARACTER

farther down in the code.
Windows 10 and Linux Mint, since 2017
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: 7056
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Add Math object to Writer by Python code

Postby barak1969 » Sun Jul 29, 2018 11:15 am

it works like magic. thanks
openoffice 4.1.5 on windows10
barak1969
 
Posts: 4
Joined: Sat Jul 28, 2018 9:15 pm


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest