user510
user510

Reputation: 111

How can I find the last non-empty column number of an Excel file using openpyxl?

How can I find the last non-empty column number of an Excel file using openpyxl?

So if the last column with data is column N, how do I get that letter and then convert it to a number?

Upvotes: 0

Views: 1128

Answers (1)

Adam Morris
Adam Morris

Reputation: 8545

You want Worksheet.max_column, which is a number.

from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['N3'] = 4
ws.max_column # returns 14

You can convert the column letter using:

from openpyxl.utils import get_column_letter, column_index_from_string
column_index_from_string('N') # => 14
get_column_letter(14) # => N

Upvotes: 2

Related Questions