Add insert image in Writer

This commit is contained in:
Mauricio Baeza 2020-10-28 23:25:46 -06:00
parent c4cae4b12c
commit 00cc16b895
1 changed files with 137 additions and 8 deletions

View File

@ -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.table.CellContentType import EMPTY, VALUE, TEXT, FORMULA
from com.sun.star.util import Time, Date, DateTime 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.awt import XActionListener
from com.sun.star.lang import XEventListener from com.sun.star.lang import XEventListener
from com.sun.star.awt import XMouseListener from com.sun.star.awt import XMouseListener
@ -492,11 +494,26 @@ def call_macro(args, in_thread=False):
def run(command, capture=False): def run(command, capture=False):
result = subprocess.run(shlex.split(command), cmd = shlex.split(command)
capture_output=capture, text=True).stdout result = subprocess.run(cmd, capture_output=capture, text=True)
if capture:
result = result.stdout
else:
result = result.returncode
return result 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): def sleep(seconds):
time.sleep(seconds) time.sleep(seconds)
return return
@ -1269,10 +1286,30 @@ class LOCalcRange(object):
def position(self): def position(self):
return self.obj.Position 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): def select(self):
self.doc.select(self.obj) self.doc.select(self.obj)
return 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): def to_size(self, rows, cols):
cursor = self.cursor cursor = self.cursor
cursor.collapseToSize(cols, rows) cursor.collapseToSize(cols, rows)
@ -1382,15 +1419,16 @@ class LOCalcRange(object):
return self.sheet.shapes[-1] return self.sheet.shapes[-1]
def insert_image(self, path, args={}): def insert_image(self, path, args={}):
s = self.obj.Size ps = self.possize
args['Width'] = args.get('Width', s.Width) args['Width'] = args.get('Width', ps['Width'])
args['Height'] = args.get('Height', s.Width) args['Height'] = args.get('Height', ps['Height'])
args['X'] = args.get('X', self.position.X) args['X'] = args.get('X', ps['X'])
args['Y'] = args.get('Y', self.position.Y) args['Y'] = args.get('Y', ps['Y'])
# ~ img.ResizeWithCell = True # ~ img.ResizeWithCell = True
img = self.sheet.dp.insert_image(path, args) img = self.sheet.dp.insert_image(path, args)
img.Anchor = self.obj img.Anchor = self.obj
return args.clear()
return img
class LOWriterPageStyle(LOBaseObject): class LOWriterPageStyle(LOBaseObject):
@ -1422,12 +1460,91 @@ class LOWriterPageStyles(object):
return '\n'.join(self.names) 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): class LOWriter(LODocument):
def __init__(self, obj): def __init__(self, obj):
super().__init__(obj) super().__init__(obj)
self._type = WRITER 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 @property
def view_cursor(self): def view_cursor(self):
return self._cc.ViewCursor return self._cc.ViewCursor
@ -2976,6 +3093,18 @@ class Paths(object):
result = Path(path).exists() result = Path(path).exists()
return result 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 @classmethod
def is_dir(cls, path): def is_dir(cls, path):
return Path(path).is_dir() return Path(path).is_dir()