From 00cc16b895dc80c9c47c570990dee8f6dcf40bf2 Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Wed, 28 Oct 2020 23:25:46 -0600 Subject: [PATCH] Add insert image in Writer --- source/easymacro2.py | 145 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 137 insertions(+), 8 deletions(-) diff --git a/source/easymacro2.py b/source/easymacro2.py index 9bb5d32..df892ef 100644 --- a/source/easymacro2.py +++ b/source/easymacro2.py @@ -57,6 +57,8 @@ from com.sun.star.sheet import TableFilterField from com.sun.star.table.CellContentType import EMPTY, VALUE, TEXT, FORMULA from com.sun.star.util import Time, Date, DateTime +from com.sun.star.text.TextContentAnchorType import AS_CHARACTER + from com.sun.star.awt import XActionListener from com.sun.star.lang import XEventListener from com.sun.star.awt import XMouseListener @@ -492,11 +494,26 @@ def call_macro(args, in_thread=False): def run(command, capture=False): - result = subprocess.run(shlex.split(command), - capture_output=capture, text=True).stdout + cmd = shlex.split(command) + result = subprocess.run(cmd, capture_output=capture, text=True) + if capture: + result = result.stdout + else: + result = result.returncode return result +# ~ def popen(command, stdin=None): + # ~ try: + # ~ proc = subprocess.Popen(shlex.split(command), shell=IS_WIN, + # ~ stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + # ~ for line in proc.stdout: + # ~ yield line.decode().rstrip() + # ~ except Exception as e: + # ~ error(e) + # ~ yield (e.errno, e.strerror) + + def sleep(seconds): time.sleep(seconds) return @@ -1269,10 +1286,30 @@ class LOCalcRange(object): def position(self): return self.obj.Position + @property + def size(self): + return self.obj.Size + + @property + def possize(self): + data = { + 'Width': self.size.Width, + 'Height': self.size.Height, + 'X': self.position.X, + 'Y': self.position.Y, + } + return data + def select(self): self.doc.select(self.obj) return + def offset(self, rows=0, cols=1): + ra = self.range_address + col = ra.EndColumn + cols + row = ra.EndRow + rows + return LOCalcRange(self.sheet[row, col].obj) + def to_size(self, rows, cols): cursor = self.cursor cursor.collapseToSize(cols, rows) @@ -1382,15 +1419,16 @@ class LOCalcRange(object): return self.sheet.shapes[-1] def insert_image(self, path, args={}): - s = self.obj.Size - args['Width'] = args.get('Width', s.Width) - args['Height'] = args.get('Height', s.Width) - args['X'] = args.get('X', self.position.X) - args['Y'] = args.get('Y', self.position.Y) + ps = self.possize + args['Width'] = args.get('Width', ps['Width']) + args['Height'] = args.get('Height', ps['Height']) + args['X'] = args.get('X', ps['X']) + args['Y'] = args.get('Y', ps['Y']) # ~ img.ResizeWithCell = True img = self.sheet.dp.insert_image(path, args) img.Anchor = self.obj - return + args.clear() + return img class LOWriterPageStyle(LOBaseObject): @@ -1422,12 +1460,91 @@ class LOWriterPageStyles(object): return '\n'.join(self.names) +class LOWriterTextRange(object): + + def __init__(self, obj, doc): + self._obj = obj + self._doc = doc + self._is_paragraph = self.obj.ImplementationName == 'SwXParagraph' + self._is_table = self.obj.ImplementationName == 'SwXTextTable' + + @property + def obj(self): + return self._obj + + @property + def string(self): + return self.obj.String + @string.setter + def string(self, value): + self.obj.String = value + + @property + def value(self): + return self.string + + @property + def is_table(self): + return self._is_table + + @property + def text(self): + return self.obj.getText() + + @property + def cursor(self): + return self.text.createTextCursorByRange(self.obj) + + def offset(self): + cursor = self.cursor.getEnd() + return LOWriterTextRange(cursor, self._doc) + + def insert_content(self, data, cursor=None, replace=False): + if cursor is None: + cursor = self.cursor + self.text.insertTextContent(cursor, data, replace) + return + + 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.GraphicURL = _P.to_url(path) + image.AnchorType = AS_CHARACTER + image.Width = w + image.Height = h + self.insert_content(image) + return + + +class LOWriterTextRanges(object): + + def __init__(self, obj, doc): + self._obj = obj + self._doc = doc + + def __getitem__(self, index): + return LOWriterTextRange(self.obj[index], self._doc) + + @property + def obj(self): + return self._obj + + class LOWriter(LODocument): def __init__(self, obj): super().__init__(obj) self._type = WRITER + @property + def selection(self): + sel = self.obj.CurrentSelection + # ~ print(sel.ImplementationName) + if sel.ImplementationName == 'SwXTextRanges': + sel = LOWriterTextRanges(sel, self) + return sel + @property def view_cursor(self): return self._cc.ViewCursor @@ -2976,6 +3093,18 @@ class Paths(object): result = Path(path).exists() return result + @classmethod + def exists_app(cls, name_app): + return bool(shutil.which(name_app)) + + @classmethod + def open(cls, path): + if IS_WIN: + os.startfile(path) + else: + pid = subprocess.Popen(['xdg-open', path]).pid + return + @classmethod def is_dir(cls, path): return Path(path).is_dir()