Add insert image from stream

This commit is contained in:
Mauricio Baeza 2021-06-29 15:09:25 -05:00
parent 80691ad797
commit 467280fca3
1 changed files with 47 additions and 11 deletions

View File

@ -26,6 +26,7 @@ import datetime
import getpass
import gettext
import hashlib
import io
import json
import logging
import os
@ -2198,7 +2199,6 @@ class LOCalcSheet(object):
@property
def draw_page(self):
return LODrawPage(self.obj.DrawPage)
@property
def dp(self):
return self.draw_page
@ -2828,7 +2828,8 @@ class LOCalcRange(object):
call_dispatch(self.doc.frame, url, args)
return self.sheet.shapes[-1]
def insert_image(self, path, args={}):
def insert_image(self, path, options={}):
args = options.copy()
ps = self.possize
args['Width'] = args.get('Width', ps['Width'])
args['Height'] = args.get('Height', ps['Height'])
@ -3572,7 +3573,8 @@ class LODrawPage(LOBaseObject):
self.obj.remove(self.obj[0])
return
def insert_image(self, path, args={}):
def insert_image(self, path, options={}):
args = options.copy()
index = self.count
w = args.get('Width', 3000)
h = args.get('Height', 3000)
@ -3581,11 +3583,17 @@ class LODrawPage(LOBaseObject):
name = args.get('Name', f'image{index}')
image = self.create_instance('com.sun.star.drawing.GraphicObjectShape')
image.GraphicURL = _P.to_url(path)
if isinstance(path, str):
image.GraphicURL = _P.to_url(path)
else:
gp = create_instance('com.sun.star.graphic.GraphicProvider')
properties = dict_to_property({'InputStream': path})
image.Graphic = gp.queryGraphic(properties)
self.obj.add(image)
image.Size = Size(w, h)
image.Position = Point(x, y)
image.Name = name
self.obj.add(image)
return LOShape(self.obj[index], index)
@ -3957,6 +3965,7 @@ class LOBasic(LODocument):
class LODocs(object):
_desktop = None
# ~ 'private:stream'
def __init__(self):
self._desktop = get_desktop()
@ -6602,13 +6611,12 @@ class Paths(object):
def image(cls, path):
# ~ sfa = create_instance('com.sun.star.ucb.SimpleFileAccess')
# ~ stream = sfa.openFileRead(cls.to_url(path))
# ~ debug(type(stream), stream)
# ~ Name = "InputStream"
# ~ Value = stream
gp = create_instance('com.sun.star.graphic.GraphicProvider')
image = gp.queryGraphic((
PropertyValue(Name='URL', Value=cls.to_url(path)),
))
if isinstance(path, str):
properties = (PropertyValue(Name='URL', Value=cls.to_url(path)),)
else:
properties = (PropertyValue(Name='InputStream', Value=path),)
image = gp.queryGraphic(properties)
return image
@classmethod
@ -6634,6 +6642,32 @@ class Dates(object):
return d
class IOStream(object):
@classmethod
def buffer(cls):
return io.BytesIO()
@classmethod
def input(cls, buffer):
instance = 'com.sun.star.io.SequenceInputStream'
stream = create_instance(instance, True)
stream.initialize((uno.ByteSequence(buffer.getvalue()),))
return stream
@classmethod
def qr(cls, data, **kwargs):
import segno
kwargs['kind'] = kwargs.get('kind', 'svg')
kwargs['scale'] = kwargs.get('scale', 8)
kwargs['border'] = kwargs.get('border', 2)
buffer = cls.buffer()
segno.make(data).save(buffer, **kwargs)
stream = cls.input(buffer)
return stream
class SpellChecker(object):
def __init__(self):
@ -6699,6 +6733,8 @@ def __getattr__(name):
return ClipBoard
if name == 'dates':
return Dates
if name == 'ios':
return IOStream()
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")