Exemplo n.º 1
0
 def test_write_anchor_onecell(self):
     from legacy_openpyxl.drawing import Image
     path = os.path.join(DATADIR, "plain.png")
     drawing = Image(path).drawing
     drawing.anchortype = "oneCell"
     drawing.anchorcol = 0
     drawing.anchorrow = 0
     root = Element("test")
     self.dw._write_anchor(root, drawing)
     xml = get_xml(root)
     expected = """<test><xdr:oneCellAnchor xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"><xdr:from><xdr:col>0</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>0</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:from><xdr:ext cx="1123950" cy="1123950"/></xdr:oneCellAnchor></test>"""
     diff = compare_xml(xml, expected)
     assert diff is None, diff
Exemplo n.º 2
0
def test_write_hyperlink_image_rels(Workbook, Image):
    wb = Workbook()
    ws = wb.create_sheet()
    ws.cell('A1').value = "test"
    ws.cell('A1').hyperlink = "http://test.com/"
    img = os.path.join(DATADIR, "plain.png")
    i = Image(img)
    ws.add_image(i)
    raise ValueError("Resulting file is invalid")
Exemplo n.º 3
0
 def test_ctor(self):
     Image = self.make_one()
     i = Image(img=self.img)
     assert i.nochangearrowheads == True
     assert i.nochangeaspect == True
     d = i.drawing
     assert d.coordinates == ((0, 0), (1, 1))
     assert d.width == 118
     assert d.height == 118
Exemplo n.º 4
0
 def test_write_anchor(self):
     from legacy_openpyxl.drawing import Image
     path = os.path.join(DATADIR, "plain.png")
     drawing = Image(path).drawing
     root = Element("test")
     self.dw._write_anchor(root, drawing)
     xml = get_xml(root)
     expected = """<test><xdr:absoluteAnchor xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"><xdr:pos x="0" y="0"/><xdr:ext cx="1123950" cy="1123950"/></xdr:absoluteAnchor></test>"""
     diff = compare_xml(xml, expected)
     assert diff is None, diff
Exemplo n.º 5
0
    def test_write_images(self):
        from legacy_openpyxl.drawing import Image
        path = os.path.join(DATADIR, "plain.png")
        img = Image(path)
        root = Element("{%s}wsDr" % SHEET_DRAWING_NS)
        self.dw._write_image(root, img, 1)
        drawing_schema.assertValid(root)
        xml = get_xml(root)
        expected = """<xdr:wsDr xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing">
  <xdr:absoluteAnchor>
    <xdr:pos x="0" y="0"/>
    <xdr:ext cx="1123950" cy="1123950"/>
    <xdr:pic>
      <xdr:nvPicPr>
        <xdr:cNvPr id="2" name="Picture 1"/>
        <xdr:cNvPicPr>
          <a:picLocks noChangeArrowheads="1" noChangeAspect="1"/>
        </xdr:cNvPicPr>
      </xdr:nvPicPr>
      <xdr:blipFill>
        <a:blip xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" cstate="print" r:embed="rId1"/>
        <a:srcRect/>
        <a:stretch>
          <a:fillRect/>
        </a:stretch>
      </xdr:blipFill>
      <xdr:spPr bwMode="auto">
        <a:xfrm>
          <a:off x="0" y="0"/>
          <a:ext cx="0" cy="0"/>
        </a:xfrm>
        <a:prstGeom prst="rect">
          <a:avLst/>
        </a:prstGeom>
        <a:noFill/>
        <a:ln w="1">
          <a:noFill/>
          <a:miter lim="800000"/>
          <a:headEnd/>
          <a:tailEnd len="med" type="none" w="med"/>
        </a:ln>
        <a:effectLst/>
      </xdr:spPr>
    </xdr:pic>
    <xdr:clientData/>
  </xdr:absoluteAnchor>
</xdr:wsDr>
"""
        diff = compare_xml(xml, expected)
        assert diff is None, diff
Exemplo n.º 6
0
def test_write_images():
    wb = Workbook()
    ew = ExcelWriter(workbook=wb)
    from legacy_openpyxl.drawing import Image
    imagepath = os.path.join(DATADIR, "plain.png")
    img = Image(imagepath)

    buf = BytesIO()

    archive = zipfile.ZipFile(buf, 'w')
    ew._write_images([img], archive, 1)
    archive.close()

    buf.seek(0)
    archive = zipfile.ZipFile(buf, 'r')
    zipinfo = archive.infolist()
    assert len(zipinfo) == 1
    assert zipinfo[0].filename == 'xl/media/image1.png'
Exemplo n.º 7
0
import os

from legacy_openpyxl import Workbook
from legacy_openpyxl.drawing import Image
from legacy_openpyxl.tests.helper import DATADIR

wb = Workbook()
ws = wb.get_active_sheet()
ws.cell('A1').value = 'You should see a logo below'

# create an image instance
pth = os.path.split(__file__)[0]
img = Image(os.path.join(DATADIR, 'plain.png'))

# place it if required
img.drawing.left = 200
img.drawing.top = 100

# you could also 'anchor' the image to a specific cell
# img.anchor(ws.cell('B12'))

# add to worksheet
ws.add_image(img)
wb.save(os.path.join(pth, 'files', 'logo.xlsx'))
Exemplo n.º 8
0
 def test_anchor_onecell(self):
     Image = self.make_one()
     i = Image(self.img)
     c = DummyCell()
     vals = i.anchor(c, anchortype="oneCell")
     assert vals == ((0, 0), None)
Exemplo n.º 9
0
 def test_anchor(self):
     Image = self.make_one()
     i = Image(self.img)
     c = DummyCell()
     vals = i.anchor(c)
     assert vals == (('A', 1), (118, 118))
Exemplo n.º 10
0
 def test_import(self):
     Image = self.make_one()
     with pytest.raises(ImportError):
         i = Image._import_image(self.img)