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
def test_ctor(self, Image, datadir): datadir.chdir() i = Image(img="plain.png") 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
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")
def test_write_anchor(self): from 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
def read_drawings(ws, drawings_path, archive, valid_files): """ Given a worksheet and the XML of its drawings file, links drawings to cells """ drawings_codename = os.path.split(drawings_path)[-1] rels_file = PACKAGE_DRAWINGS_RELS + '/' + drawings_codename + '.rels' if rels_file not in valid_files: return None rels_source = archive.read(rels_file) rels_root = fromstring(rels_source) root = fromstring(archive.read(drawings_path)) for node in root: col, row = 0, 0 name = u'' cell_from = node.find('{%s}from' % SHEET_DRAWING_NS) if cell_from is not None: col = cell_from.find('{%s}col' % SHEET_DRAWING_NS) if col is not None: col = int(col.text) row = cell_from.find('{%s}row' % SHEET_DRAWING_NS) if row is not None: row = int(row.text) cell = ws['%s%s' % (get_column_letter(col + 1), row + 1)] pic = node.find('{%s}pic' % SHEET_DRAWING_NS) if pic is not None: nv_pic_pr = pic.find('{%s}nvPicPr' % SHEET_DRAWING_NS) if nv_pic_pr is not None: nv_pic_pr = nv_pic_pr.find('{%s}cNvPr' % SHEET_DRAWING_NS) if nv_pic_pr is not None: name = nv_pic_pr.attrib.get('name', '') blip_fill = pic.find('{%s}blipFill' % SHEET_DRAWING_NS) if blip_fill is not None: blip = blip_fill.find('{%s}blip' % DRAWING_NS) if blip is not None: rid = blip.attrib.get('{%s}embed' % REL_NS) if rid is not None: image_file = read_image_file(rels_root, rid, valid_files) if image_file: img = Image(BytesIO(archive.read(image_file))) img.drawing.name = name img.anchor(cell, anchortype='oneCell') ws.add_image(img)
def test_write_anchor_onecell(self): from 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
def test_write_images(self): from 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
def test_write_images(): wb = Workbook() ew = ExcelWriter(workbook=wb) from 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'
# Start new event currentEvent = row['Event'] print(currentEvent) output = load_workbook('masters/{}.xlsx'.format( row['Event'].lower())) sheets = [sheet for sheet in output] wsCount = 0 if currentCategory != row['Category']: # Start new spreadsheet currentCategory = row['Category'] ws = sheets[wsCount] wsCount += 1 # Add image img = Image('nbta.png') # place image relative to top left corner of spreadsheet img.drawing.top = 10 img.drawing.left = 30 ws.add_image(img) ws.title = row['Category'][:26] ws['C3'] = row['Category'] i = 5 # Add names to worksheet ws['B{}'.format(i)] = row['Name'] i += 1 # Finish last event
def test_anchor_onecell(self, Image, datadir): datadir.chdir() i = Image("plain.png") c = DummyCell() vals = i.anchor(c, anchortype="oneCell") assert vals == ((0, 0), None)
def test_anchor(self, Image, datadir): datadir.chdir() i = Image("plain.png") c = DummyCell() vals = i.anchor(c) assert vals == (('A', 1), (118, 118))
def ImageFile(datadir, Image): datadir.chdir() return Image("plain.png")
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)
def test_anchor(self): Image = self.make_one() i = Image(self.img) c = DummyCell() vals = i.anchor(c) assert vals == (('A', 1), (118, 118))
import os from openpyxl import Workbook from openpyxl.drawing import Image from openpyxl.tests.helper import DATADIR wb = Workbook() ws = wb.active 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'))