Issues with special characters in sheet names?
Created originally on Bitbucket by axfelix (Alex Garnett)
Hi folks,
I'm trying to do the following:
workbook = openpyxl.load_workbook(workbookname)
sheets = workbook.get_sheet_names()
totalsheets = len(sheets)
if totalsheets!=1:
workbooks = {}
for y in sheets:
if y != "DV-IDENTITY-0":
print y
workbooks[y] = workbook
for x in sheets:
print x
if x != y:
workbooks[y].remove_sheet(workbooks[y].get_sheet_by_name(x))
such that it creates a new workbook containing only one worksheet for each of the worksheets in the original workbook. the "print" commands are in there for sanity checking right now. I'm having a really weird issue... the output currently is:
sheet1
sheet1
sheet2
sheet3&funnyname
sheet4
sheet2
sheet1
sheet2
sheet3&funnyname
Traceback (most recent call last):
File "csvnormalize.py", line 34, in <module>
workbooks[y].remove_sheet(workbooks[y].get_sheet_by_name(x))
File "/usr/local/lib/python2.7/dist-packages/openpyxl/workbook.py", line 161, in remove_sheet
self.worksheets.remove(worksheet)
ValueError: list.remove(x): x not in list
I can't understand why it's failing on sheet3&funnyname while it's looping through the second new workbook but not the first ... any idea?