Adding Worksheet Background Image
Hello, I would like to contribute a PR to add a public method to the Worksheet
class that allows you to add a background image to a given worksheet. I will call the entry point Worksheet.add_background(img)
unless there is any objection and a different function name used.
This implements Excel's background functionality:
This adds an image to the xl/media
Which is then represented as a Relationship for the given worksheet. In this example xl/worksheets/_rels/sheet1.xml.rels
:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image1.jpeg"/>
</Relationships>
The picture is then referenced in the xl/worksheets/sheet1.xml
via the <worksheet><picture>
element with the r:id
tag:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
<dimension ref="A1"/>
<sheetViews>
<sheetView tabSelected="1" workbookViewId="0"/>
</sheetViews>
<sheetFormatPr defaultRowHeight="14.4" x14ac:dyDescent="0.3"/>
<sheetData/>
<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
<picture r:id="rId1"/>
</worksheet>
I have been successful in integrating this functionality into openpyxl and would like to submit a PR. I have never worked with mercurial before but I am sure it will be easy enough with SourceTree.
In my implementation I added two things to Worksheet
class:
-
self._background = None
in the_setup()
method -
add_background(img)
method which sets self._background as the given image (where img isopenpyxl.drawing.image.Image
class)
For the ExcelWriter
:
- created new
_write_background(self, image)
method (where image isopenpyxl.drawing.image.Image
class) that appends the given image toself._images
and sets the image's_id
- added logic in
_write_worksheets()
method that calls_write_background
ifws
(Worksheet) has_background
set as an image.
Finally, in the WorksheetWriter
class:
- created new
write_background(self)
method that checks if worksheet as background image set, then creates aRelationship
oftype
image with theTarget
being the worksheet's backgroundImage
path. The relationship gets appended to the WorksheetWriter's_rels
list. ARelated
object is then created for said Relationship and it is then serialized to thepicture
element viaself.xf.send(picture.to_tree("picture"))
. - added new
write_background()
method inWorksheetWriter.write_tail()
call
Couple Questions:
- Can you think of any other components that would need to be accounted for?
- Do I need/where do I add a test for this?