Search code examples
pythonxlwings

Change Sheet name dainamically in Python


in my excel file I have multiple sheets and every sheet name should be the cell value of A1. For example there is a sheet abc in which cell value of A1 is xyz, so sheet name should be changed to xyz. Please advise how can I achive this. I am trying this code-

import xlwings as xw
file = 'C:\\Users\\xxx\\Downloads\\Trading.xlsx'
wb = xw.Book(file)
for sheet in wb:
    sheet_name = sheet.title
    first_cell_value = str(sheet['A1'].value)
    sheet.title = first_cell_value
    print(sheet.title)

Solution

  • A quick solution for this :)

    import xlwings as xw
    file = 'C:\\Users\\xxx\\Downloads\\Trading.xlsx'
    wb = xw.Book(file)
    for sheet in wb.sheets:
        sheet.name = sheet['A1'].value
    wb.save()