TemplatedWorkbook

The TemplatedWorkbook is our representation of the excel file and describes the file as a whole. To create a TemplatedWorkbook extend the base class and declare which sheets it includes using TemplatedWorksheets.

from openpyxl_templates import TemplatedWorkbook, TemplatedWorksheet


class DemoTemplatedWorkbook(TemplatedWorkbook):
    sheet1 = TemplatedWorksheet()
    sheet2 = TemplatedWorksheet()

To use your template to generate new excel files simply create an instance…

templated_workbook = DemoTemplatedWorkbook()

… or provide it with a filename (or a file) to read an existing one.

templated_workbook = DemoTemplatedWorkbook(file="my_excel.xlsx")

The TemplatedWorkbook will find all sheets which correspond to a TemplatedWorksheet. Once identified the TemplatedWorksheets can be used to interact with the underlying excel sheets. The matching is done based on the sheetname. The TemplatedWorkbook keeps track of the declaration order of the TemplatedWorksheets which enables it to make sure the the sheets are always in the correct order once the file has been saved. The identified sheets can also be iterated as illustrated below.

for templated_worksheet in templated_workbook.templated_sheets:
    print(templated_worksheet.sheetname)

To save the workbook simply call the save method and provide a filename

templated_workbook.save("my_excel.xlsx")