From cdbb00fad4e8826087de86911a2860f10d7945a5 Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Sat, 10 Jul 2021 17:29:29 -0500 Subject: [PATCH] Start support for insert math in writer --- CHANGELOG | 5 +++++ doc/source/main/examples_writer.rst | 14 ++++++++++++-- source/easymacro.py | 23 ++++++++++++++++------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 3cb28e5..5b936b8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,7 +1,12 @@ +v 0.17.0 [10-jul-2021] + - Add insert math in writer + + v 0.16.1 [01-jul-2021] - Change property is_connected in db - Update doc + v 0.16.0 [20-jun-2021] - Add connection for postgres and mariadb - Fix in call macro Basic diff --git a/doc/source/main/examples_writer.rst b/doc/source/main/examples_writer.rst index d7f3067..a584e17 100644 --- a/doc/source/main/examples_writer.rst +++ b/doc/source/main/examples_writer.rst @@ -1,9 +1,9 @@ For Writer -------------------- +---------- Set autostyle in table -^^^^^^^^^^^^^^^^^^^^^^ +~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: python @@ -12,3 +12,13 @@ Set autostyle in table table = doc.tables[0] table.style = 'Academic' return + + +Insert math formula +~~~~~~~~~~~~~~~~~~~ + +.. code-block:: python + + formula = '%LAMBDA_{deg","t}=1 + %alpha_deg SQRT {M_t over M_{(t=0)}-1}~"."' + text = app.selection + text.insert_math(formula) diff --git a/source/easymacro.py b/source/easymacro.py index 1b848a1..b125752 100644 --- a/source/easymacro.py +++ b/source/easymacro.py @@ -164,13 +164,23 @@ OBJ_RANGE = 'ScCellRangeObj' OBJ_RANGES = 'ScCellRangesObj' TYPE_RANGES = (OBJ_CELL, OBJ_RANGE) -OBJ_SHAPES = 'com.sun.star.drawing.SvxShapeCollection' OBJ_SHAPE = 'com.sun.star.comp.sc.ScShapeObj' +OBJ_SHAPES = 'com.sun.star.drawing.SvxShapeCollection' OBJ_GRAPHIC = 'SwXTextGraphicObject' OBJ_TEXTS = 'SwXTextRanges' OBJ_TEXT = 'SwXTextRange' +CLSID = { + 'FORMULA': '078B7ABA-54FC-457F-8551-6147e776a997', +} + +SERVICES = { + 'TEXT_EMBEDDED': 'com.sun.star.text.TextEmbeddedObject', + 'TEXT_TABLE': 'com.sun.star.text.TextTable', + 'GRAPHIC': 'com.sun.star.text.GraphicObject', +} + # ~ from com.sun.star.sheet.FilterOperator import EMPTY, NO_EMPTY, EQUAL, NOT_EQUAL class FilterOperator(IntEnum): @@ -3310,11 +3320,9 @@ class LOWriterTextRange(object): def insert_math(self, formula, anchor_type=TextContentAnchorType.AS_CHARACTER, cursor=None, replace=False): - CLSID = '078B7ABA-54FC-457F-8551-6147e776a997' - service = 'com.sun.star.text.TextEmbeddedObject' - math = self._doc.create_instance(service) - math.CLSID = CLSID + math = self._doc.create_instance(SERVICES['TEXT_EMBEDDED']) + math.CLSID = CLSID['FORMULA'] math.AnchorType = anchor_type self.insert_content(math, cursor, replace) math.EmbeddedObject.Component.Formula = formula @@ -3327,7 +3335,7 @@ class LOWriterTextRange(object): return self._doc.selection def insert_table(self, data): - table = self._doc.create_instance('com.sun.star.text.TextTable') + table = self._doc.create_instance(SERVICES['TEXT_TABLE']) rows = len(data) cols = len(data[0]) table.initialize(rows, cols) @@ -3340,7 +3348,8 @@ class LOWriterTextRange(object): def insert_image(self, path, args={}): w = args.get('Width', 1000) h = args.get('Height', 1000) - image = self._doc.create_instance('com.sun.star.text.GraphicObject') + + image = self._doc.create_instance(SERVICES['GRAPHIC']) image.GraphicURL = _P.to_url(path) image.AnchorType = TextContentAnchorType.AS_CHARACTER image.Width = w