Home > OS >  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?

Time:01-03

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?

CodePudding user response:

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
  • Related