我正在尝试将 excel 中的坐标值转换为 openpyxl 中的行号和列号。
例如,如果我的单元格坐标是 D4,我想找到相应的行号和列号以用于将来的操作,在这种情况下,行 = 3,列 = 3。我可以使用 ws 轻松获取行号.cell('D4').row
返回 4
然后只需减去 1。但是类似的参数 ws.cell('D4').column
返回 D
并且我不知道如何轻松地将其转换为 int 形式以进行后续操作。所以我求助于stackoverflow的聪明人。你能帮帮我吗?
最佳答案
你想要的是 openpyxl.utils.coordinate_from_string()
和 openpyxl.utils.column_index_from_string()
from openpyxl.utils.cell import coordinate_from_string, column_index_from_string
xy = coordinate_from_string('A4') # returns ('A',4)
col = column_index_from_string(xy[0]) # returns 1
row = xy[1]
关于python - 从openpyxl中的坐标值获取行号和列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12902621/