This code allows a user to write large quantities of Python data to spreadsheet rows or columns.
Ensure the following are installed on the machine you are running the application on:
- Python 2.7 with Pip
- virtualenv for Python 2.7
Create a virtualenv and install the requirements from requirements.txt with pip:
$ virtualenv venv
$ source venv/bin/activate
(venv)$ pip install -r "requirements.txt"
Spreadsheets can be opened by instantiating a Spreadsheet object with the name of the sheet. The extension is optional.
>>> from Spreadsheet import Spreadsheet
>>> sheet = Spreadsheet("test_sheet.xlsx")
Cells can be read with the Spreadsheet.read_row() and Spreadsheet.read_column() functions. Functions return a dictionary containing the read values. Row and column indices begin at 1.
>>> print sheet.read_row(row=1, start_col=1)
{u'Row 1': [u'Row', u'Text']}
>>> print sheet.read_column(col=1, header=True, start_row=1)
{u'Row': [u'Column', u'Text']}
If header=True in the Spreadsheet.read_column() method, The first value will be considered the header and used as the column's dictionary key (see above example).
Open spreadsheets can be written or appended to.
Spreadsheets can be written to with the Spreadsheet.write_row() and Spreadsheet.write_column() methods. The content parameter must be a list of strings. Each string in the list will be written to a concurrent cell.
>>> sheet.write_row(row=1, content=['These', 'Are', 'Headers'], start_col=1, bold=True, italics=False)
# Writes These, Are, and Headers in bold to the top three cells horizontally in bold.
>>> sheet.write_column(col=1, content=['Here', 'Are', 'Values'], start_row=2, bold=False, italics=True)
# Writes Here, Are, and Values in italics to three columns under the 'These' heading.
Spreadsheets can be appended to with the Spreadsheet.append_row() and Spreadsheet.append_column() methods. The content parameter must be a list of strings. Each string in the list will be written to a concurrent cell.
>>> sheet.append_row(row=1, content=['More', 'Up', 'Top'], bold=True, italics=False)
# Writes More, Up, and Top in bold after 'Headers.'
>>> sheet.append_column(col=1, content=['More', 'In', 'Col'], bold=False, italics=True)
# Writes More, In, and Col in italics to three columns under 'Values.'
Spreadsheets can be saved with the Spreadsheet.save() method. Sheets are saved automatically when they are written to.
>>> sheet.save()
# Saves the sheet
- Spreadsheet.py - Class for reading and writing to spreadsheets.