从openpyxl中的坐标值获取行号和列号 [英] getting the row and column numbers from coordinate value in openpyxl

查看:2039
本文介绍了从openpyxl中的坐标值获取行号和列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将excel中的坐标值转换为openpyxl中的行号和列号.

I'm trying to covert a coordinate value in excel to a row number and column number in openpyxl.

例如,如果我的单元格坐标是D4,我想找到相应的行和列号以用于将来的操作,在row = 3,column = 3的情况下,我可以使用ws.cell('D4').row轻松获得行号如果返回4,则只需要减去1.但是类似的参数ws.cell('D4').column返回D,我不知道如何轻松地将其转换为int形式以用于后续操作.因此,我向您介绍stackoverflow的明智伙伴.你能帮我吗?

For example if my cell coordinate is D4 I want to find the corresponding row and column numbers to use for future operations, in the case row = 3, column = 3. I can get the row number easily using ws.cell('D4').row which returns 4 then it's just a matter of subtracting 1. But a similar argument ws.cell('D4').column returns D and I don't know how to easily get this into int form for subsequent operations. So I turn to you wise folks of stackoverflow. Can you help me?

推荐答案

您想要的是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]

这篇关于从openpyxl中的坐标值获取行号和列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆