简体   繁体   中英

How assign value to one cell in named range by using xlwings?

I'm a novice with both Python and xlwings too. I need to assign a different value for each cell in named range step by step.

xw.Range('Named range').value = [new_cell_values_as_list_of_lists]

This is an example how to assign values to full range Named range . This is not what I need for now.

I've searched the xlwings-docs, stackoverflow and so but without any idea... Ways neither

xw.Range(1,'Named range', 22).value = 'yes'

nor

xw.Range(1, 'Named range', '22').value = 'yes' 

do not work. Please help me.

In a future version of xlwings, you should be able to use slice notation on a Range directly, but for now, probably the easiest and most efficient method (only one read and write operation) is to read in the whole Range, do manipulations on the Python side and write the whole Range back:

values = xw.Range(1, 'Named range').value
values[1][1] = 'yes'
xw.Range(1, 'Named range').value = values

Alternatively, depending on your situation, this might also be a possibility:

first_col = xw.Range(1, 'Named range').column
first_row = xw.Range(1, 'Named range').row
xw.Range(1, (first_col + 1, first_row + 1)).value = 'yes'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM