libxlsxwriter
|
Libxlsxwriter positions worksheet objects such as images and charts in worksheets by calculating precise co-ordinates based on the object size, it's DPI (for images) and any scaling that the user specifies. It also takes into account the heights and widths of the rows and columns that the object crosses. In this way objects maintain their original sizes even if the rows or columns underneath change size or are hidden.
For example:
As can be seen the inserted images are the same size even though the second image crosses changed rows and columns.
However, there are two cases where the image scale may change with row or columns changes. These are explained in the next two sections.
The scaling of a image may be affected if is crosses a row that has its default height changed due to a font that is larger than the default font size or that has text wrapping turned on. In these cases Excel will automatically calculate a row height based on the text when it loads the file. Since this row height isn't available to Libxlsxwriter when it creates the file the object may appear as if it is sized incorrectly. For example::
As can be seen the second inserted image is distorted, compared to the first, due to the row being scaled automatically. To avoid this you should explicitly set the height of the row using worksheet_set_row()
if it crosses an inserted object.
Excel supports three options for "Object Positioning" within a worksheet:
Image and chart objects in Libxlsxwriter emulate these options using the object_position
parameter in lxw_image_options and lxw_chart_options:
Where object_position
has one of the following allowable values from lxw_object_position:
Option 4 appears in Excel as Option 1. However, the worksheet object is sized to take hidden rows or columns into account. This allows the user to hide an image in a cell, possibly as part of an autofilter. For example:
In this example the first inserted image is visible over the hidden rows whilst the second image is hidden with the rows. Unhiding the rows in Excel would reveal the second image.
When an image is imported into Excel the DPI (dots per inch) resolution of the image is taken into account. Excel sizes the image according to a base DPI of 96 dpi. Therefore an image with a DPI of 72 may appear slightly larger when imported into Excel while an image with a DPI of 200 may appear twice as small. Libxlsxwriter also reads the DPI of the images that the user inserts into a worksheet and stores the image dimensions in the same way that Excel does. If it cannot determine the DPI of the image it uses a default of 96.
A lot of work has gone into ensuring that Libxlsxwriter inserts images into worksheets in exactly the same way that Excel does, even though the required calculations and units are arcane. There are over 80 test cases that check image insertion against files created in Excel to ensure that Libxlsxwriter's handling of images is correct.
As such, before reporting any issues with image handling in Libxlsxwriter please check how the same image is handled in Excel (not OpenOffice, LibreOffice or other third party applications). If you do report an issue please use the Libxlsxwriter Issue tracker is on GitHub that demonstrates the issue.
Next: Working with Autofilters