valign images in xlsxwriter cell

Viewed 98

Is there any hack for v-aligning images in a cell

  • I am trying to create a dashboard,those traffic lights are images.
  • Since once of the columns is a text-wrap and the height of those rows are dynamic, I have no way of knowing the row height to calculate the y_offset for those images
  • Does anyone have a recommendation on how I can handle this? Is there a way of getting the row_height after sheet.write and text_wrap format is applied?

enter image description here

1 Answers

Is there a way of getting the row_height after sheet.write and text_wrap format is applied?

Probably not without access to Windows APIs for calculating bounding boxes for strings.

You could probably make some working estimates based on the length of your string. Each new line in text wrap is equal to 15 character units or 20 pixels.

Since once of the columns is a text-wrap and the height of those rows are dynamic, I have no way of knowing the row height to calculate the y_offset for those images

This is the main problem. In order to specify the image position exactly you will need to specify explicit row heights so that XlsxWriter can calculate where the image will go based on the size of the cell. In order words you will have to avoid the automatic row height that Excel gives you when wrapping text.

Once the row height is fixed you can position images exactly where you want them using the 'x_offset' and 'y_offset' options.

Note, you can also use conditional formatting to create traffic lights based on cell values. See Sheet9/Example 9 of this code from the XlsxWriter docs and image below. These can be centered automatically even with with text wrapping.

enter image description here

Related