Convert an excel or spreadsheet column letter to its number in Pythonic fashion

前端 未结 17 1225
夕颜
夕颜 2020-12-09 04:18

Is there a more pythonic way of converting excel-style columns to numbers (starting with 1)?

Working code up to two letters:



        
17条回答
  •  执念已碎
    2020-12-09 04:47

    You could just add the following to the console after installing the openpyxl module:

    import openpyxl
    from openpyxl.utils import get_column_letter, column_index_from_string
    workbook = openpyxl.load_workbook('your_workbook.xlsx')
    sheet = wb.get_sheet_by_name('your_sheet_from_workbook')
    print(get_column_letter(1))
    print(column_index_from_string('A'))
    

    Just change the letters and number to suit your needs or create a for-loop to do the job for a whole project. I hope this helps. Cheers.

提交回复
热议问题