python - 从openpyxl中的坐标值获取行号和列号

标签 python openpyxl

我正在尝试将 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/

相关文章:

python - 检查模型输入 : expected no data, 时出错,但得到:

python - 打印 Excel 工作表时如何将重复的前 2 行设置为标题?

python - 使用 openpyxl 模块在 python 中取消合并 _cell

python - 在 Openpyxl 中设置样式

python - 导入 openpyxl 时语法无效

python - 为什么我不能在 pandas 为我创建的 xlsxwriter 对象上卡住 Pane ?

Python:检查/dev/disk 设备是否存在

python - 使用重复索引按索引 reshape Pandas 数据帧

python - 编写一个 pytest 函数来检查控制台上的输出(stdout)

python - openpyxl 如果包含数据则跳过该行