Skip to main content

Apply cell formatting to gspread worksheets

Project description

gspread-formatting

https://badge.fury.io/py/gspread-formatting.svg

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.

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)])

CellFormat objects are comparable with == and !=, and are mutable at all times; they can be safely copied with copy.deepcopy. 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

The 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.

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)

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

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.0.3.tar.gz (6.9 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.0.3-py2.py3-none-any.whl (10.2 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: gspread-formatting-0.0.3.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/2.7

File hashes

Hashes for gspread-formatting-0.0.3.tar.gz
Algorithm Hash digest
SHA256 8883fdd70fe7bbbe97f3f50eec63a68abba91b4bbe75a22d8547e618c33b028b
MD5 b98525db36a8e6de60cc88fa2a71c6e0
BLAKE2b-256 4d2d82084d7ebac7575b5086223f9af0ac80c5300e832948f2d2ff2c2d63f11b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gspread_formatting-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fd060d06282402059f9f3a08247362ea831886db507d6198674fee15abe43735
MD5 f03cf883a8e336d61160f9da1c6f3ffb
BLAKE2b-256 bedf0d49b776d2bf9069f1441eebb6252aed8762d72524ae98252130f181e754

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