From 2069ac0ba8466d6075986525736c754b1dd4c3e1 Mon Sep 17 00:00:00 2001 From: El Mau Date: Sun, 7 Jan 2024 21:37:52 -0600 Subject: [PATCH] Add tables for Writer --- source/easymacro/constants.py | 7 +- source/easymacro/easycalc.py | 46 +++++---- source/easymacro/easydrawpage.py | 4 - source/easymacro/easyshape.py | 29 +++++- source/easymacro/easyuno.py | 4 + source/easymacro/easywriter.py | 168 +++++++++++++++++++++++++++++++ 6 files changed, 232 insertions(+), 26 deletions(-) diff --git a/source/easymacro/constants.py b/source/easymacro/constants.py index c81473a..ba19d06 100644 --- a/source/easymacro/constants.py +++ b/source/easymacro/constants.py @@ -4,18 +4,21 @@ # ~ https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1sheet_1_1CellFlags.html from com.sun.star.sheet import CellFlags - -from .easyuno import BitmapMode, FillStyle, LineStyle +from .easyuno import BitmapMode, CellContentType, FillStyle, LineStyle __all__ = [ 'ALL', 'ONLY_DATA', 'BitmapMode', + 'CellContentType', 'FillStyle', 'LineStyle', ] +# ~ VALUE, DATETIME, STRING, ANNOTATION, FORMULA ONLY_DATA = 31 ALL = 1023 + +SECONDS_DAY = 86400 \ No newline at end of file diff --git a/source/easymacro/easycalc.py b/source/easymacro/easycalc.py index a7bfa0b..dcbd1f6 100644 --- a/source/easymacro/easycalc.py +++ b/source/easymacro/easycalc.py @@ -4,9 +4,6 @@ import datetime from decimal import Decimal from typing import Any, Union -from com.sun.star.sheet import CellFlags -from com.sun.star.table.CellContentType import EMPTY, VALUE, TEXT, FORMULA - from .easymain import (log, DATE_OFFSET, BaseObject, Color, LOMain, dict_to_property, run_in_thread, set_properties @@ -17,15 +14,9 @@ from .easyshape import LOShapes, LOShape from .easydrawpage import LODrawPage from .easyforms import LOForms from .easystyles import LOStyleFamilies +from .constants import ONLY_DATA, SECONDS_DAY, CellContentType -SECONDS_DAY = 60 * 60 * 24 -ONLY_VALUES = CellFlags.VALUE + CellFlags.DATETIME + CellFlags.STRING - - -# ~ IsFiltered, -# ~ IsManualPageBreak, -# ~ IsStartOfNewPage class LOSheetRows(): def __init__(self, sheet, obj, range_address): @@ -45,7 +36,7 @@ class LOSheetRows(): return self.obj.Count def __str__(self): - name = f'Rows: {self._ra.StartRow} - {self._ra.EndRow}' + name = f'Rows: {self._ra.StartRow} to {self._ra.EndRow}' return name @property @@ -59,6 +50,27 @@ class LOSheetRows(): def visible(self, value): self._obj.IsVisible = value + @property + def is_filtered(self): + return self._obj.IsFiltered + @is_filtered.setter + def is_filtered(self, value): + self._obj.IsFiltered = value + + @property + def is_manual_page_break(self): + return self._obj.IsManualPageBreak + @is_manual_page_break.setter + def is_manual_page_break(self, value): + self._obj.IsManualPageBreak = value + + @property + def is_start_of_new_page(self): + return self._obj.IsStartOfNewPage + @is_start_of_new_page.setter + def is_start_of_new_page(self, value): + self._obj.IsStartOfNewPage = value + @property def color(self): return self.obj.CellBackColor @@ -215,9 +227,7 @@ class LOCalcRange(): pass def __len__(self): - ra = self.range_address - rows = ra.EndRow - ra.StartRow + 1 - return rows + return self.obj.Rows.Count def __str__(self): s = f'Range: {self.name}' @@ -375,11 +385,11 @@ class LOCalcRange(): def value(self): """Get or set value, automatically get type data""" v = None - if self.type == VALUE: + if self.type == CellContentType.VALUE: v = self.float - elif self.type == TEXT: + elif self.type == CellContentType.TEXT: v = self.string - elif self.type == FORMULA: + elif self.type == CellContentType.FORMULA: v = self.formula return v @value.setter @@ -535,7 +545,7 @@ class LOCalcRange(): c2 = ra.EndColumn + 1 return LOCalcRange(self.sheet[r1:r2, c1:c2].obj) - def clear(self, what: int=ONLY_VALUES): + def clear(self, what: int=ONLY_DATA): """Clear contents""" # ~ http://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1sheet_1_1CellFlags.html self.obj.clearContents(what) diff --git a/source/easymacro/easydrawpage.py b/source/easymacro/easydrawpage.py index e30c16f..2e61b72 100644 --- a/source/easymacro/easydrawpage.py +++ b/source/easymacro/easydrawpage.py @@ -21,10 +21,6 @@ TYPE_SHAPES = ('Line', 'Measure', 'Rectangle', 'Ellipse', 'Text', 'Connector', # ~ class LOShapeBK(BaseObject): - # ~ @property - # ~ def cell(self): - # ~ return self.anchor - # ~ @property # ~ def anchor(self): # ~ obj = self.obj.Anchor diff --git a/source/easymacro/easyshape.py b/source/easymacro/easyshape.py index f3c93fe..c39864e 100644 --- a/source/easymacro/easyshape.py +++ b/source/easymacro/easyshape.py @@ -5,7 +5,7 @@ from typing import Any from com.sun.star.awt import Size, Point from .easymain import ( BaseObject, Paths, - create_instance, dict_to_property, set_properties, get_properties + create_instance, dict_to_property, log, set_properties, get_properties ) from .easyuno import ( BaseObjectProperties, @@ -14,11 +14,13 @@ from .easyuno import ( IMAGE = 'com.sun.star.drawing.GraphicObjectShape' + MIME_TYPE = { 'image/png': 'png', 'image/jpeg': 'jpg', 'image/svg': 'svg', } + TYPE_MIME = { 'svg': 'image/svg', 'png': 'image/png', @@ -72,9 +74,32 @@ class LOShape(BaseObjectProperties): return f'Shape: {self.name}' @property + def sheet(self): + return self.anchor + @sheet.setter + def sheet(self, value): + self.anchor = value + @property + def cell(self): + return self.anchor + @cell.setter + def cell(self, value): + self.anchor = value + @property def anchor(self): + from .easycalc import LOCalcSheet, LOCalcRange + TYPE_ANCHOR = { + 'ScTableSheetObj': LOCalcSheet, + 'ScCellObj': LOCalcRange, + } """Get anchor object""" - return self.obj.Anchor + obj = self.obj.Anchor + implementation = obj.ImplementationName + if implementation in TYPE_ANCHOR: + obj = TYPE_ANCHOR[implementation](obj) + else: + log.debug(implementation) + return obj @anchor.setter def anchor(self, obj): if hasattr(obj, 'obj'): diff --git a/source/easymacro/easyuno.py b/source/easymacro/easyuno.py index d4dce6f..a21ce5f 100644 --- a/source/easymacro/easyuno.py +++ b/source/easymacro/easyuno.py @@ -28,6 +28,10 @@ class BitmapMode(): from com.sun.star.drawing.BitmapMode import REPEAT, STRETCH, NO_REPEAT +class CellContentType(): + from com.sun.star.table.CellContentType import EMPTY, VALUE, TEXT, FORMULA + + class IOStream(object): """Classe for input/output stream""" diff --git a/source/easymacro/easywriter.py b/source/easymacro/easywriter.py index a07dcf3..2fa4487 100644 --- a/source/easymacro/easywriter.py +++ b/source/easymacro/easywriter.py @@ -8,6 +8,170 @@ from .easydrawpage import LODrawPage from .easystyles import LOStyleFamilies +class LOTableRange(BaseObject): + + def __init__(self, table, obj): + self._table = table + super().__init__(obj) + + def __str__(self): + return f'TextTable: Range - {self.name}' + + @property + def name(self): + if self.is_cell: + n = self.obj.CellName + else: + c1 = self.obj[0,0].CellName + c2 = self.obj[self.rows-1,self.columns-1].CellName + n = f'{c1}:{c2}' + return n + + @property + def is_cell(self): + return hasattr(self.obj, 'CellName') + + @property + def data(self): + return self.obj.getDataArray() + @data.setter + def data(self, values): + self.obj.setDataArray(values) + + @property + def rows(self): + return len(self.data) + + @property + def columns(self): + return len(self.data[0]) + + @property + def string(self): + return self.obj.String + @string.setter + def string(self, value): + self.obj.String = value + + @property + def value(self): + return self.obj.Value + @value.setter + def value(self, value): + self.obj.Value = value + + +class LORow(BaseObject): + + def __init__(self, rows, index): + self._rows = rows + self._index = index + super().__init__(rows[index]) + + def __str__(self): + return 'TextTable: Row' + + @property + def height(self): + return self.obj.Height + @height.setter + def height(self, value): + self.obj.Height = value + + def remove(self): + self._rows.removeByIndex(self._index, 1) + return + + +class LORows(BaseObject): + + def __init__(self, obj): + super().__init__(obj) + + def __str__(self): + return 'TextTable: Rows' + + def __len__(self): + return self.obj.Count + + def __getitem__(self, key): + return LORow(self.obj, key) + + @property + def count(self): + return self.obj.Count + + def remove(self, index, count=1): + self.obj.removeByIndex(index, count) + return + + +class LOTextTable(BaseObject): + + def __init__(self, obj): + super().__init__(obj) + + def __str__(self): + return f'Writer: TextTable - {self.name}' + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + pass + + def __iter__(self): + self._i = 0 + return self + + def __next__(self): + """Interation cells""" + try: + name = self.obj.CellNames[self._i] + except IndexError: + raise StopIteration + self._i += 1 + return self[name] + + def __getitem__(self, key): + if isinstance(key, str): + if ':' in key: + rango = self.obj.getCellRangeByName(key) + else: + rango = self.obj.getCellByName(key) + elif isinstance(key, tuple): + if isinstance(key[0], slice): + rango = self.obj.getCellRangeByPosition( + key[1].start, key[0].start, key[1].stop-1, key[0].stop-1) + else: + rango = self.obj[key] + return LOTableRange(self.obj, rango) + + @property + def name(self): + return self.obj.Name + @name.setter + def name(self, value): + self.obj.Name = value + + @property + def rows(self): + return LORows(self.obj.Rows) + + +class LOTextTables(BaseObject): + + def __init__(self, obj): + super().__init__(obj) + + def __str__(self): + return 'Writer: TextTables' + + def __getitem__(self, key): + return LOTextTable(self.obj[key]) + + + class LOWriterTextPortion(BaseObject): def __init__(self, obj): @@ -180,6 +344,10 @@ class LOWriter(LODocument): def view_cursor(self): return self._cc.ViewCursor + @property + def tables(self): + return LOTextTables(self.obj.TextTables) + def select(self, rango: Any): """""" obj = rango