Search code examples
pythonexcelpandasdataframexlwings

Xlwings - unable to insert values for a range of rows


I am trying to insert a value n into a specific H column from position H10:H20

So, I tried the below

start_index = int(10)
for n in range(20):
    print(type(n))  # this returns int
    range('H' + str(start_index)).value = n
    start_index = start_index + 1 

However, the above code results in the below error

      2 for n in range(20):
      3     print(type(n))
----> 4     range('H' + str(start_index)).value = n
      5     start_index = start_index + 1

TypeError: 'str' object cannot be interpreted as an integer

But my n is an integer.


Solution

  • Try the following from xlwings import range as xlrange and rename the code at line 4 as xlrange.

    Or use import xlwings and at line 4 use xlwings.range.

    Try to avoid asterisk in your import statements in order to avoid polluting the namespace. For more info check this post