Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to create a new worksheet using xlwings?

Using Python xlwings, how can I create a new worksheet?

like image 369
hd810 Avatar asked Oct 14 '14 11:10

hd810


People also ask

Which is better Openpyxl or xlwings?

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.

What can xlwings do?

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.


2 Answers

import xlwings as xw
wb = xw.Book()
wb.sheets.add()

See also the docs.

like image 168
Felix Zumstein Avatar answered Sep 20 '22 19:09

Felix Zumstein


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.

like image 42
Robert Avatar answered Sep 20 '22 19:09

Robert