Resolver One Library Documentation

API for Workbook

Back to documentation index page.

Classes

Class Workbook

class Workbook(object):
    def __init__(self, path=None):

'Workbook' is the class that represents a spreadsheet and contains its worksheets. It has methods for creating, managing and accessing the various different types of worksheets.

A workbook is created for you at the start of the spreadsheet generated code.

You access the worksheets contained in a workbook using a dictionary-like syntax, using the worksheet name as the key. Trying to access a worksheet that does not exist will raise a KeyError.

The following code fetches the worksheet called 'Sheet1' and stores it in a variable called 'sheet_1':

sheet_1 = workbook['Sheet1']

Worksheets accessed by name are not case sensitive, this also applies to all the workbook methods for adding worksheets.

Methods

AddDatabaseTriggerListener

def AddDatabaseTriggerListener(self, server, database, table):

This function enables automatic recalculation of spreadsheets when a database is updated. It does this by listening for 'triggers' from the database. (The appropriate stored procedure or trigger must be installed on the database server so that the appropriate information is sent when the database is updated.)

To listen for a trigger from a particular database, call AddDatabaseTriggerListener with three strings: the server name, the database name and the table name.

(Note that this can be setup to happen automatically, by ticking using the appropriate box when using the 'Insert Database Worksheet' option in the GUI. You are of course free to do it manually from user code if you wish.)

This method does not return anything, but will recalculate your workbook when changes occur to your database.

Invalid input (for example using non-strings) will not raise exceptions, but means that database updates will not be received.

AddImageWorksheet

def AddImageWorksheet(self, name, image):

AddImageWorksheet adds an image in a new worksheet in your workbook using the specified name (string) and Image object. The worksheet will display the image you give it.

The image object must be an instance of the Image class (or a subclass like Bitmap) from the System.Drawing assembly [1].

The following code adds an image worksheet to the workbook, using an image.

from System.Drawing import Bitmap
image = Bitmap.FromFile('C:\\Documents and Settings\\My Picture.PNG')
workbook.AddImageWorksheet('My Picture', image)

This method returns an ImageWorksheet instance.

The example above ignores the return value, as you can always access the worksheet by name on the workbook. Alternatively you can keep a reference to the new worksheet and use that.

The worksheet name will have '(IMAGE)' shown in front of it in the spreadsheet tabs.

Attempting to add an image worksheet with the same name as a non-image worksheet will raise a TypeError.

If there is already an image worksheet with the same name, then the old image will be replaced with the new one.

[1]See http://msdn2.microsoft.com/en-us/library/system.drawing.image(vs.80).aspx

AddWorksheet

def AddWorksheet(self, name, mode=None):

It can be useful to create a worksheet in user code if you are importing data from external sources but if you do create a worksheet from user code you cannot edit the worksheet in the grid. You would have to make all adjustments in user code.

This method will add a worksheet to a workbook, using a specified name (string). If a worksheet of the same name already exists, then this method will return a reference to the existing worksheet rather than adding a new one.

This method returns a Worksheet instance.

The mode argument specifies what sort of worksheet should be created.

If mode is set to WorksheetMode.Cache then a cache worksheet is obtained. This worksheet will not be discarded between recalculations, and so can be used to carry values from one recalculation to the next. It will, however, be discarded when the document is closed.

If a cache worksheet already exists with this name then the existing cache worksheet will be added.

If the first call to AddWorksheet with a given name during a recalc adds a worksheet with the same name as a cache worksheet but with a different mode, then the cache worksheet will be discarded.

A previously cached worksheet must be added in each recalculation for it to be accessible via the workbook but the worksheet will not be discarded unless the document is closed, a worksheet is added with the same name, or the cache worksheet is explicitly cleared.

Cache worksheets will be discarded upon closing the document.

It is common to ignore the return value, as you can always access the worksheet by name on the workbook. Alternatively, you can keep a reference to the new worksheet and use that.

newWorksheet = workbook.AddWorksheet('New Worksheet')
newWorksheet.A1 = 3

If the worksheet is created from user code, then the new worksheet name will show '(RESULT)' in the spreadsheet tabs. You can't modify worksheets created like this by typing into them or changing row heights / column width from the user interface. You can modify cells and col / row attributes from your user code however.

Attempting to add a worksheet with the same name as an existing image worksheet will raise a TypeError.

Attempting to add a worksheet with an explicit mode (either WorksheetMode.Normal or WorksheetMode.Cache) when a worksheet of that name has already been added, but with a different mode, will raise a TypeError.

AddWorksheetFromDB

def AddWorksheetFromDB(self, name, connectString, query, showColumnNames=False):

This method creates a new worksheet from a database query. It takes a worksheet name (string), database connection string, SQL query string, and a boolean (optional, and defaulting to False) specifying whether or not to show the column names.

This method returns a Worksheet instance.

connectString = (
    "DRIVER={MySQL ODBC 3.51 Driver};"
    "SERVER=book_server;"
    "PORT=3306;"
    "DATABASE=my_books;"
    "USER=username;"
    "PASSWORD=password;"
    "OPTION=3;"
)

query = "SELECT title FROM books WHERE price > 2 ORDER BY title"

Sheet4 = workbook.AddWorksheetFromDB("Sheet4", connectString, query)

If 'ShowColumnNames' is True, then the column names will be put in row 1 (with the data starting in row 2). Row 1 will be set as the header row for the worksheet and made bold.

If fetching from the database fails for any reason, a DatabaseError exception will be raised.

AddWorksheetFromDataSource

def AddWorksheetFromDataSource(self, name, dataSource, query, showColumnNames=False):

This creates a new worksheet from a database query in the same way as AddWorksheetFromDB, but using a named data source (string) instead of a connection string.

The named data source string should be the name of a valid ODBC data source on your computer. You can configure these from Control Panel -> Administrative Tools -> Data Sources (ODBC) (under Windows XP).

This method returns a Worksheet instance.

If fetching from the data source fails for any reason, a DatabaseError exception will be raised.

Populate

def Populate(self, constants, formatting):

This method takes two 'dictionaries of dictionaries' and populates the worksheets in a workbook with, values and formatting information.

It is not intended that this method be called from user code, but is called from generated spreadsheet code to efficiently populate worksheets with their constants and formatting.

Calling this method with invalid input will result in undefined behaviour, and may corrupt your worksheets.

This method does not return anything.

__iter__

def __iter__(self):

When iterating over a 'Workbook', it returns every worksheet in the workbook, except for image worksheets.