0

I am trying to create a loop to add sheets to an excel file based on names in a list. For some reason it is not working. Would appreciate some help.

import openpyxl

worksheets = ['Balance Sheet Entries',
              'Production Costs',
              'Model - Realised',
              'Orders',
              'Manufacturing Comparison',
              'Unit Economics']

# File where new sheets should be created
template = openpyxl.load_workbook('New File.xlsx')

#Loop through worksheets and create one new sheet using each name in the list.
for i in range(len(worksheets)):
    template.create_sheet(worksheets[i]) #or template.create_sheet(title=worksheets[i])

The outcome of running this is exactly nothing. Nothing happens to the file and the code runs fine "Process finished with exit code 0".

Any help appreciated.

2
  • 1
    The pythonic way to loop through a list is for worksheet in worksheets. Commented Sep 1, 2020 at 21:32
  • @SuperStormer yes, thanks for that. I'm still a beginner on this. Commented Sep 2, 2020 at 8:42

1 Answer 1

2

A quick scan through the docs suggests you need to call template.save(filename) in order to actually save to disk.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.