LE: Any method to select all the existing sheets from a workbook using pywin32?
I am opening an Excel workbook using win32com.client.
I want the user to be able to select a number of sheets.
How can I transform the user's input into a list?
I tried:
USER_INP = simpledialog.askstring(title="Sheets to be converted",
prompt="How many sheets do you need?")
print(USER_INP)
z = str(USER_INP)
ws_index_list = []
if z == 1:
ws_index_list = [1]
elif z == 2:
ws_index_list = [1,2]
elif z == 3:
ws_index_list = [1,2,3]
elif z == 4:
ws_index_list = [1,2,3,4]
elif z == 5:
ws_index_list = [1,2,3,4,5]
elif z == 6:
ws_index_list = [1,2,3,4,5,6]
elif z == 7:
ws_index_list = [1,2,3,4,5,6,7]
elif z == 8:
ws_index_list = [1,2,3,4,5,6,7,8]
elif z == 9:
ws_index_list = [1,2,3,4,5,6,7,8,9]
elif z == 10:
ws_index_list = [1,2,3,4,5,6,7,8,9,10]
But it does not work. It works if I give it a predefined list such as:
ws_index_list = [1,2]
Error:
line 75, in <module>
wb.WorkSheets(ws_index_list).Select()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\dynamic.py", line 186, in __call__
return self._get_good_object_(self._oleobj_.Invoke(*allArgs),self._olerepr_.defaultDispatchName,None)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352571), 1)
Also, why if I want to export my file as "final test 2" it gets the name "final%test%2". How can I replace the "%?"
PS: I know that my code is a mess, that's why I am asking for help :) hopefully I won't get any downvotes
Solved
USER_INP = simpledialog.askstring(title="Sheets to be converted",
prompt="How many sheets do you need?")
z = str(USER_INP)
ws_index_list = []
if z == str(1):
ws_index_list.extend([1])
elif z == str(2):
ws_index_list.extend([1,2])
elif z == str(3):
ws_index_list.extend([1,2,3])
elif z == str(4):
ws_index_list.extend([1,2,3,4])
elif z == str(5):
ws_index_list.extend([1,2,3,4,5])
elif z == str(6):
ws_index_list.extend([1,2,3,4,5,6])
elif z == str(7):
ws_index_list.extend([1,2,3,4,5,6,7])
elif z == str(8):
ws_index_list.extend([1,2,3,4,5,6,7,8])
elif z == str(9):
ws_index_list.extend([1,2,3,4,5,6,7,8,9])
elif z == str(10):
ws_index_list.extend([1,2,3,4,5,6,7,8,9,10])