Using Python xlwings
, how can I create a new worksheet?
xlwings is the better choice if you want to split the design and code work. XlsxWriter/OpenPyxl is the better choice if it needs to be scalable and run on a server. If you need to generate PDF files at high speed, check out ReportLab.
Xlwings is a Python library that makes it easy to call Python from Excel and vice versa. It creates reading and writing to and from Excel using Python easily. It can also be modified to act as a Python Server for Excel to synchronously exchange data between Python and Excel.
import xlwings as xw
wb = xw.Book()
wb.sheets.add()
See also the docs.
I use the following helper function to add a sheet if it is not yet existing or get/activate it, in case it is already present:
def addActivate(wb, sheetName, template=None):
try:
sht = wb.sheets(sheetName).activate()
except com_error:
if template:
template.sheets["Template"].api.Copy(wb.sheets.active.api)
sht = wb.sheets["Template"].api.Name = sheetName
else:
sht = wb.sheets.add(sheetName)
return sht
It also allows to define the name of a sheet name to be used as template for the new sheet.
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