-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexcel_tab_renamer.py
41 lines (29 loc) · 936 Bytes
/
excel_tab_renamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
### Variables
file_in = input("Enter File Path of Excel Workbook:")
### Code
def tab_renamer (input):
### Imports
import openpyxl as xl
from pathlib import Path
### Open Excel Workbook
wb = xl.load_workbook(input)
### Create List of New Names based on new_names tab
new_names = []
sheet = wb['new_names']
for row in sheet.rows:
new_names.append(row[0].value)
print(new_names)
## Change Tab Names
old_names = wb.sheetnames
old_names.remove("new_names") # Get List of Current Tab Names
print(old_names)
if len(old_names) == len(new_names):
i = 0
for old_name in old_names:
wb[old_name].title = new_names[i]
i+=1
wb.save(str(Path(input).parent/Path(input).stem) + '_edited' + str(Path(input).suffix))
else:
print('Number of New Names don\'t match Number of Tabs')
### Run Code
tab_renamer (file_in)