Google Spreadsheets Python API v4
76
Create and manage charts within worksheets with various chart types and customization options for data visualization.
Add charts to worksheets with data ranges and customization options.
class Worksheet:
def add_chart(self, data, start=None, end=None, chart_type=ChartType.COLUMN, **kwargs) -> Chart:
"""
Add chart to worksheet.
Parameters:
- data: Data range for chart (Address, GridRange, or string)
- start (str): Start cell for chart placement
- end (str): End cell for chart placement
- chart_type (ChartType): Type of chart to create
- **kwargs: Additional chart options (title, domain, ranges, etc.)
Returns:
Chart: New chart object
"""
def get_charts(self) -> list:
"""
Get all charts in worksheet.
Returns:
list: List of Chart objects
"""Access and modify chart properties, appearance, and data sources.
class Chart:
def __init__(self, worksheet, chart_data=None):
"""
Initialize chart object.
Parameters:
- worksheet (Worksheet): Parent worksheet
- chart_data (dict): Chart data from API
"""
@property
def id(self) -> int:
"""Chart ID."""
@property
def title(self) -> str:
"""Chart title."""
@title.setter
def title(self, title):
"""Set chart title."""
@property
def chart_type(self) -> str:
"""Chart type."""
@chart_type.setter
def chart_type(self, chart_type):
"""Set chart type."""
@property
def domain(self) -> dict:
"""Chart domain (x-axis) range."""
@domain.setter
def domain(self, domain):
"""Set chart domain range."""
@property
def ranges(self) -> list:
"""Chart data series ranges."""
@ranges.setter
def ranges(self, ranges):
"""Set chart data series ranges."""
@property
def anchor_cell(self) -> str:
"""Chart anchor cell position."""
@anchor_cell.setter
def anchor_cell(self, cell):
"""Set chart anchor cell position."""
@property
def legend_position(self) -> str:
"""Chart legend position."""
@legend_position.setter
def legend_position(self, position):
"""Set chart legend position."""Customize chart appearance including fonts, colors, and layout options.
class Chart:
@property
def title_font_family(self) -> str:
"""Chart title font family."""
@title_font_family.setter
def title_font_family(self, font_family):
"""Set chart title font family."""
@property
def font_name(self) -> str:
"""Chart font name."""
@font_name.setter
def font_name(self, font_name):
"""Set chart font name."""
def update_chart(self):
"""
Update chart by applying any changes made to chart properties.
"""
def refresh(self):
"""Refresh chart data from API."""
def delete(self):
"""Delete chart from worksheet."""Manage chart data sources and series configuration.
class Chart:
def get_json(self) -> dict:
"""
Get chart configuration as JSON.
Returns:
dict: Complete chart configuration
"""
def set_json(self, chart_json):
"""
Set chart configuration from JSON.
Parameters:
- chart_json (dict): Chart configuration JSON
"""import pygsheets
# Get worksheet with data
gc = pygsheets.authorize()
sh = gc.open('Sales Data')
wks = sh.sheet1
# Add sample data
data = [
['Month', 'Sales', 'Profit'],
['Jan', 1000, 200],
['Feb', 1200, 250],
['Mar', 1100, 220],
['Apr', 1300, 280]
]
wks.update_values('A1', data)
# Create column chart
chart = wks.add_chart(
data=pygsheets.GridRange.create('A1', 'C5'),
chart_type=pygsheets.ChartType.COLUMN,
title='Monthly Sales and Profit'
)
# Position chart
chart.anchor_cell = 'E2'# Create line chart with customization
chart = wks.add_chart(
data='A1:C5',
chart_type=pygsheets.ChartType.LINE,
title='Sales Trend'
)
# Customize appearance
chart.title = 'Monthly Sales Trend Analysis'
chart.title_font_family = 'Arial'
chart.font_name = 'Arial'
chart.legend_position = 'RIGHT_LEGEND'
# Set domain (x-axis) and ranges (y-axis series)
chart.domain = pygsheets.GridRange.create('A2', 'A5') # Month column
chart.ranges = [
pygsheets.GridRange.create('B2', 'B5'), # Sales data
pygsheets.GridRange.create('C2', 'C5') # Profit data
]
# Apply updates
chart.update_chart()# Create different chart types
data_range = 'A1:C5'
# Bar chart
bar_chart = wks.add_chart(
data=data_range,
chart_type=pygsheets.ChartType.BAR,
title='Horizontal Bar Chart'
)
bar_chart.anchor_cell = 'E2'
# Area chart
area_chart = wks.add_chart(
data=data_range,
chart_type=pygsheets.ChartType.AREA,
title='Area Chart'
)
area_chart.anchor_cell = 'E15'
# Scatter plot
scatter_chart = wks.add_chart(
data='B1:C5', # Sales vs Profit
chart_type=pygsheets.ChartType.SCATTER,
title='Sales vs Profit Correlation'
)
scatter_chart.anchor_cell = 'E28'
# Combo chart
combo_chart = wks.add_chart(
data=data_range,
chart_type=pygsheets.ChartType.COMBO,
title='Combo Chart'
)
combo_chart.anchor_cell = 'H2'# Get all charts in worksheet
charts = wks.get_charts()
print(f"Found {len(charts)} charts")
# Update existing chart
if charts:
chart = charts[0]
chart.title = 'Updated Chart Title'
chart.chart_type = pygsheets.ChartType.LINE
chart.update_chart()
# Export chart configuration
chart_config = chart.get_json()
print("Chart configuration:", chart_config)
# Clone chart configuration
new_chart_config = chart_config.copy()
new_chart_config['title'] = 'Cloned Chart'
# Delete charts
for chart in charts[1:]: # Keep first chart, delete others
chart.delete()# Create combo chart with mixed chart types
combo_chart = wks.add_chart(
data='A1:C5',
chart_type=pygsheets.ChartType.COMBO,
title='Sales (Columns) and Profit (Line)'
)
# Configure combo chart series
combo_config = {
'title': 'Mixed Chart Types',
'series': [
{'type': 'COLUMN', 'targetAxis': 'LEFT_AXIS'},
{'type': 'LINE', 'targetAxis': 'RIGHT_AXIS'}
]
}
combo_chart.set_json(combo_config)
# Stepped area chart
stepped_chart = wks.add_chart(
data='A1:B5',
chart_type=pygsheets.ChartType.STEPPED_AREA,
title='Stepped Area Chart'
)class ChartType:
BAR = 'BAR'
LINE = 'LINE'
AREA = 'AREA'
COLUMN = 'COLUMN'
SCATTER = 'SCATTER'
COMBO = 'COMBO'
STEPPED_AREA = 'STEPPED_AREA'class LegendPosition:
BOTTOM_LEGEND = 'BOTTOM_LEGEND'
LEFT_LEGEND = 'LEFT_LEGEND'
RIGHT_LEGEND = 'RIGHT_LEGEND'
TOP_LEGEND = 'TOP_LEGEND'
NO_LEGEND = 'NO_LEGEND'Install with Tessl CLI
npx tessl i tessl/pypi-pygsheetsdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10