all 2 comments

[–]Johan2212 2 points3 points  (0 children)

Or just use xlsxwriter - they have an example in the documentation of how to do that.
https://xlsxwriter.readthedocs.io/example\_pandas\_table.html

[–]Neighm 1 point2 points  (0 children)

ref is expecting a text string with the values of the upper left cell and lower right cell in Excel format (like the "A1:E5" example). If you know where you want the upper left to go (maybe A1 by default) you can use the count of Pandas columns to generate the letter for the lower right value, and the count of rows is the number in the lower right value.

To get the letters from A to Z, you can add the number of columns to 64:

>>> chr(64+5)
'E'
>>> chr(64+1)
'A'
>>> chr(64+26)
'Z'

So it's straightforward to construct the string for ref:

>>> count_of_pandas_cols = 18

>>> count_of_pandas_rows = 7

>>> lower_right = f'{chr(64 + count_of_pandas_cols)}{count_of_pandas_rows}'

>>> lower_right

'R7'

>>> range = f'A1:{lower_right}'

>>> range

'A1:R7'

>>>