Convert Excel row,column indices to alphanumeric cell reference in python/openpyxl

2024/10/6 4:09:37

I want to convert the row and column indices into an Excel alphanumeric cell reference like 'A1'. I'm using python and openpyxl, and I suspect there's a utility somewhere in that package that does this, but I haven't found anything after some searching.

I wrote the following, which works, but I'd rather use something that's part of the openpyxl package if it's available.

def xlref(row,column):"""xlref - Simple conversion of row, column to an excel string format>>> xlref(0,0)'A1'>>> xlref(0,26)'AA1'"""def columns(column):from string import uppercaseif column > 26**3:raise Exception("xlref only supports columns < 26^3")c2chars = [''] + list(uppercase)c2,c1 = divmod(column,26)c3,c2 = divmod(c2,26)return "%s%s%s" % (c2chars[c3],c2chars[c2],uppercase[c1])return "%s%d" % (columns(column),row+1)

Does anyone know a better way to do this?

Answer

Here's the full new xlref using openpyxl.utils.get_column_letter from @Rick's answer:

from openpyxl.utils import get_column_letterdef xlref(row, column, zero_indexed=True):if zero_indexed:row += 1column += 1return get_column_letter(column) + str(row)

Now

>>> xlref(0, 0)
'A1'
>>> xlref(100, 100)
'CW101'
https://en.xdnf.cn/q/70404.html

Related Q&A

Flask-admin - how to change formatting of columns - get URLs to display

Question on flask-admin. I setup flask-admin and one of the models i created is pulling urls and url titles from a mysql database. Using flask-admin, how to i get flask-admin to render the urls instea…

Stream audio from pyaudio with Flask to HTML5

I want to stream the audio of my microphone (that is being recorded via pyaudio) via Flask to any client that connects.This is where the audio comes from:def getSound(self):# Current chunk of audio dat…

Adding into Path var while silent installation of Python - possible bug?

I need to passively install Python in my applications package installation so i use the following:python-3.5.4-amd64.exe /passive PrependPath=1according this: 3.1.4. Installing Without UI I use the Pre…

Pandas add new columns based on splitting another column

I have a pandas dataframe like the following:A B US,65,AMAZON 2016 US,65,EBAY 2016My goal is to get to look like this:A B country code com US.65.AMAZON 2016…

Proper way to insert HTML into Flask

I know how to send some plain text from Python with render_template when a URL is visited:@app.route(/success.html") ... return render_template("index.html", text="This text goes in…

How to add a new class to an existing classifier in deep learning?

I trained a deep learning model to classify the given images into three classes. Now I want to add one more class to my model. I tried to check out "Online learning", but it seems to train on…

Count unique elements along an axis of a NumPy array

I have a three-dimensional array likeA=np.array([[[1,1], [1,0]],[[1,2], [1,0]],[[1,0], [0,0]]])Now I would like to obtain an array that has a nonzero value in a given position if only a unique nonzero …

influxdb python: 404 page not found

I am trying to use the influxdb-python lib which I found here. But I cant even get the tutorial programm to work. When I run the following example code:$ python>>> from influxdb import InfluxD…

Django Table already exist

Here is my Django Migration file. When I run python manage.py makemigrations/migrate I get this error.Error:-django.db.utils.OperationalError: (1050, "Table tickets_duration already exists")I…

Python round() too slow, faster way to reduce precision?

I am doing the following:TOLERANCE = 13 some_float = ... round(some_float, TOLERANCE)This is run many times, so performance is important. I have to round some_float due to floating point representation…