Worksheet set title unexpected behaviour when title starts with "Sheet" string

Issue #509 resolved
Tomasz Zięba created an issue

Sometimes openpyxl worksheet tilte is different with given string after simple set:

my_worksheet_title = "Sheet2"
worksheet = wb.create_sheet()
worksheet.title = my_worksheet_title

Problem occurs for titles starting with "Sheet" prefix. I wrote python script with assersions which presents some cases when problem occured. I attach my file to this issue.


AssertionError: List of exsisting worksheet titles:
['Sheet', 'Foo', 'Baz', 'Sheet2', 'Sheet31', 'Bar', 'Sheet41', 'Sheet6']
['Sheet', 'Foo', 'Baz', 'Sheet2', 'Sheet3', 'Bar', 'Sheet4', 'Sheet6']

Piece of code for above example:

titles = ["Foo", "Baz", "Sheet2", "Sheet3", "Bar", "Sheet4", "Sheet6"]
workbook = openpyxl.Workbook()

for title in titles:
    worksheet = workbook.create_sheet()
    worksheet.title = title

titles.insert(0, 'Sheet')

assert workbook.get_sheet_names() == titles, \
    "List of exsisting worksheet titles:\n{}\nexpected:\n{}".format(
        workbook.get_sheet_names(), titles

Comments (5)

  1. Hamza Khchine

    Hi, When we create a new sheet , Openpyxl set sheet title as "Sheet", "Sheet1","Sheet3"... juste like what MS Excel does. In your case , when you create the 4th sheet , Openpyxl set the sheet title as "Sheet3" and then you set the title as "Sheet3". Openpyxl consider that we have already that title in the workbook, so to prevent duplication ,openpyxl add "1" au title.

    I made a correction in my fork 509 correction

  2. Log in to comment