I am trying to figure out how to pass several selected cells into python via xlwings. I found code that passes cell notation but not all of the values.
For example: In my worksheet, I selected cells D8, D12, D18, and D21 having values of cell1, cell2, cell3, cell4
Code:
wb = xw.Book.caller()
dbg = wb.sheets['debug']
selected_range = wb.selection
Print results in my debug worksheet
dbg.range((1, 1)).value = selected_range.address
This is what gets printed: $D$8,$D$12,$D$18,$D$21
What I am looking for are the actual values of the cells.
I tried another example:
cellRange = wb.app.selection
rowNum = cellRange.row
colNum = cellRange.column
dbg.range((2, 1)).value = len(cellRange), rowNum, colNum
This is what printed
$D$8,$D$12,$D$18,$D$21 1, 8, 4
The 1, 8, 4 corresponds to
cellRange = 1
rowNum = 8
colNum = 4
This says $D$8,$D$12,$D$18,$D$21 is one object and rowNum/colNum point to the first cell of the 4 I selected.
What am I missing? I want to return the values into my code to act on.
Thanks
Dan
user26262113 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.