On Wed, Apr 23, 2014 at 5:32 AM, Ketil Froyn <
ke...@froyn.name> wrote:
> Thanks for getting back to me. I've been having trouble with both the
> calculations and messed up image dimensions, actually.
OK. I've just done a few experiments with the python.bmp and image.py
included with xlwt in the examples directory. It would appear that
the final rendering of the images has vertical scaling which is
dependent on row height. If you expand a row that is "occupied" by
the image, then the image also expands. You can sort of compensate by
setting a vertical scale factor on the insert_image() call that is
sufficiently small. But it's tricky. If you change the height of
multiple occupied rows, both affect the rendered height of the image.
Futzing around with column widths appears to be benign (I didn't
notice any effect on the rendered image width).
I don't know if the interaction between row height and rendered image
height is intrinsic to the file format, or if it's a bug in xlwt, or
perhaps a bug in the documentation for the file format. In any case,
right now I would say it's too difficult to properly size rows to
match images with xlwt.
So you've got a few choices. If sticking to the (genuine, binary)
.xls format is a requirement, and you must use Python to generate it,
then xlwt is pretty much the only game in town. If this fits your
situation, then my recommendation is to not mess with the row heights,
and move your other data around instead. In other words, if you have
an image that is 5 standard rows high, then instead of making a single
row that is 5 times the standard height to hold it, just leave the row
heights the way they are and move the rest of your data down by 4
rows.
Now, I say "genuine, binary" .xls format because in plenty of
contexts, you can use some other format (like HTML) and just slap
'.xls' at the end of the file name, and things will work fine. I
haven't had to spoof the file extension myself, so I don't have much
to offer if you go that route.
If you are not tied to .xls and are open to generating .xlsx, then you
have a very attractive option in XlsxWriter. Its API is not
drastically different from xlwt (and usually where they differ,
XlsxWriter is friendlier), and it supports a lot more functionality
than xlwt. In particular, you can insert PNG, JPEG, or BMP; and from
my quick, informal testing, adjusting row or column sizes does not
affect the rendering of images.
If you go with XlsxWriter, you have to use different sizing
calculations, but that is a relatively minor issue (and I can help
with that if it comes to it).
John Y.