Skip to main content

Apply cell formatting to gspread worksheets

Project description

gspread-formatting

https://badge.fury.io/py/gspread-formatting.svg https://travis-ci.org/robin900/gspread-formatting.svg?branch=master

This package provides complete support of basic cell formatting for Google spreadsheets to the popular gspread package, along with a few related features such as setting “frozen” rows and columns in a worksheet.

The package also offers graceful formatting of Google spreadsheets using a Pandas DataFrame. See the section below for usage and details.

Usage

Basic formatting of a range of cells in a worksheet is offered by the format_cell_range function. All basic formatting components of the v4 Sheets API’s CellFormat are present as classes in the gspread_formatting module, available both by InitialCaps names and camelCase names: for example, the background color class is BackgroundColor but is also available as backgroundColor, while the color class is Color but available also as color. Attributes of formatting components are best specified as keyword arguments using camelCase naming, e.g. backgroundColor=.... Complex formats may be composed easily, by nesting the calls to the classes.

See the CellFormat page of the Sheets API documentation to learn more about each formatting component.:

from gspread_formatting import *

fmt = cellFormat(
    backgroundColor=color(1, 0.9, 0.9),
    textFormat=textFormat(bold=True, foregroundColor=color(1, 0, 1)),
    horizontalAlignment='CENTER'
    )

format_cell_range(worksheet, 'A1:J1', fmt)

The format_cell_ranges function allows for formatting multiple ranges with corresponding formats, all in one function call and Sheets API operation:

fmt = cellFormat(
    backgroundColor=color(1, 0.9, 0.9),
    textFormat=textFormat(bold=True, foregroundColor=color(1, 0, 1)),
    horizontalAlignment='CENTER'
    )

fmt2 = cellFormat(
    backgroundColor=color(0.9, 0.9, 0.9),
    horizontalAlignment='RIGHT'
    )

format_cell_ranges(worksheet, [('A1:J1', fmt), ('K1:K200', fmt2)])

Retrieving, Comparing, and Composing CellFormats

A Google spreadsheet’s own default format, as a CellFormat object, is available via get_default_format(spreadsheet). get_effective_format(worksheet, label) and get_user_entered_format(worksheet, label) also will return for any provided cell label either a CellFormat object (if any formatting is present) or None.

CellFormat objects are comparable with == and !=, and are mutable at all times; they can be safely copied with Python’s copy.deepcopy function. CellFormat objects can be combined into a new CellFormat object using the add method (or + operator). CellFormat objects also offer difference and intersection methods, as well as the corresponding operators - (for difference) and & (for intersection).:

>>> default_format = CellFormat(backgroundColor=color(1,1,1), textFormat=textFormat(bold=True))
>>> user_format = CellFormat(textFormat=textFormat(italic=True))
>>> effective_format = default_format + user_format
>>> effective_format
CellFormat(backgroundColor=color(1,1,1), textFormat=textFormat(bold=True, italic=True))
>>> effective_format - user_format
CellFormat(backgroundColor=color(1,1,1), textFormat=textFormat(bold=True))
>>> effective_format - user_format == default_format
True

Frozen Rows and Columns

The following functions get or set “frozen” row or column counts for a worksheet:

get_frozen_row_count(worksheet)
get_frozen_column_count(worksheet)
set_frozen(worksheet, rows=1)
set_frozen(worksheet, cols=1)
set_frozen(worksheet, rows=1, cols=0)

Getting and Setting Data Validation Rules for Cells and Cell Ranges

The following functions get or set the “data validation rule” for a cell or cell range:

get_data_validation_rule(worksheet, label)
set_data_validation_for_cell_range(worksheet, range, rule)
set_data_validation_for_cell_ranges(worksheet, ranges)

The full functionality of data validation rules is supported: all of BooleanCondition. See the API documentation for more information. Here’s a short example:

validation_rule = DataValidationRule(
    BooleanCondition('ONE_OF_LIST', ['1', '2', '3', '4']),
    showCustomUi=True
)
set_data_validation_for_cell_range(worksheet, 'A2:D2', validation_rule)
# data validation for A2
eff_rule = get_data_validation_rule(worksheet, 'A2')
eff_rule.condition.type
>>> 'ONE_OF_LIST'
eff_rule.showCustomUi
>>> True
# No data validation for A1
eff_rule = get_data_validation_rule(worksheet, 'A1')
eff_rule
>>> None

Conditional Formatting Rules

Conditional format rules are supported by this package! See the Conditional Format Rules docs.

Formatting a Worksheet Using a Pandas DataFrame

If you are using Pandas DataFrames to provide data to a Google spreadsheet – using perhaps the gspread-dataframe package available on PyPI – the format_with_dataframe function in gspread_formatting.dataframe allows you to use that same DataFrame object and specify formatting for a worksheet. There is a DEFAULT_FORMATTER in the module, which will be used if no formatter object is provided to format_with_dataframe:

from gspread_formatting.dataframe import format_with_dataframe, BasicFormatter
from gspread_formatting import Color

# uses DEFAULT_FORMATTER
format_with_dataframe(worksheet, dataframe, include_index=True, include_column_header=True)

formatter = BasicFormatter(
    header_background_color=Color(0,0,0),
    header_text_color=Color(1,1,1),
    decimal_format='#,##0.00'
)

format_with_dataframe(worksheet, dataframe, formatter, include_index=False, include_column_header=True)

Installation

Requirements

  • Python 2.6+ or Python 3+

  • gspread >= 3.0.0

From PyPI

pip install gspread-formatting

From GitHub

git clone https://github.com/robin900/gspread-formatting.git
cd gspread-formatting
python setup.py install

Development and Testing

Install packages listed in requirements-dev.txt. To run the test suite in test.py you will need to:

  • Authorize as the Google account you wish to use as a test, and download a JSON file containing the credentials. Name the file creds.json and locate it in the top-level folder of the repository.

  • Set up a tests.config file using the tests.config.example file as a template. Specify the ID of a spreadsheet that the Google account you are using can access with write privileges.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gspread-formatting-0.1.0.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gspread_formatting-0.1.0-py2.py3-none-any.whl (16.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file gspread-formatting-0.1.0.tar.gz.

File metadata

  • Download URL: gspread-formatting-0.1.0.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.5

File hashes

Hashes for gspread-formatting-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4415abef36cb738501b6375285871e367a42e2a2eab11925322e1f999797b27b
MD5 2b9b026b48d333cf02fc82150e523ed4
BLAKE2b-256 db93e3b56edaafc5aa8f643b74c029d6074ea159075fc51c084f9ce0eb1e1d0c

See more details on using hashes here.

File details

Details for the file gspread_formatting-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: gspread_formatting-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.5

File hashes

Hashes for gspread_formatting-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8a428f0a70a0d328879b85e9ccf9bfe3e8bf221cc0313aaf31a7dd5c43f50ef5
MD5 fef39da538cad3be30f41eb0d7dcfc56
BLAKE2b-256 866d9fe25c4bd87787610052df95b2592635e688c2971b310bb6d63ad24e1a0d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page