from openpyxl import Workbook
book = Workbook()
for country in "IN US JP UK".split():
book.create_sheet(title=country)
This creates 5 sheets with the first sheet being unwanted.
I know that I can do this:
book = Workbook()
us_ws = book.active
us_ws.title = "IN"
for country in "US JP UK".split():
book.create_sheet(title=country)
Is there any short cut?
From the (Docs)
A workbook is always created with at least one worksheet. You can get it by using the openpyxl.workbook.Workbook.active() property
So if you do not need the default sheet, you can delete it:
from openpyxl import Workbook
book = Workbook()
book.remove(book.active)
for country in "IN US JP UK".split():
book.create_sheet(title=country)
This deletes the first sheet, which will be the default created sheet, before creating the desired sheets.
Or, as pointed out in comments, if you are OK with the caveats of a write-only workbook you can do:
from openpyxl import Workbook
book = Workbook(write_only=True)
for country in "IN US JP UK".split():
book.create_sheet(title=country )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With