pythonperformancexlwingsxlsm

Fast writing to xlsm with xlwings


I am trying to write a number of floats and strings to an xlsm file using xlwings 0.20.2.

Though the following works, I want it to be faster:

sht.range('A1').options(transpose=True).value = [float1, float2]
sht.range('B7').value = string1
sht.range('A4').options(transpose=True).value = [string2, float3]
...

I already recognised that

sht.range('A1').options(transpose=True).value = [float1, float2]

is faster than

sht.range('A1').value = float1
sht.range('A2').value = float2

Thus my idea was to collect all floats and strings in a single list of lists:

output = [[None] * 8, [None] * 8]
output[0][0] = float1
output[0][1] = float2
output[1][8] = string1
... 

and write that to the Excel sheet.

The problem with that is that each time I hit a None in the write-out process, it erases the content of the cell in question (e.g. A3).

Do you have any suggestions to avoid that or ideas for an alternative approach? As I said, it is all about performance I am asking.

I wrote a custom converter like the one described here, but it is simply returning no value in the write_value() method and if the value is None it still erases the cell's content.


Solution

  • You will need to read in the whole range, then change those values you want on the Python side and write it back. If your range contains formulas, you can use the myrange.formula property to read the formulas-it's ok to write them out via .value again. This way, you won't change existing cells.