CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-xlwings

Python library for seamless bidirectional communication between Python and Excel across Windows and macOS platforms.

Overview
Eval results
Files

charts-visualization.mddocs/

Charts and Visualization

Classes for creating and manipulating Excel charts, including support for matplotlib figure integration and chart customization. xlwings provides comprehensive charting capabilities that bridge Python's visualization libraries with Excel's native charting engine.

Capabilities

Chart Objects

Excel charts created and manipulated through xlwings, supporting various chart types and customization options.

class Chart:
    """Represents an Excel chart object."""
    
    def delete(self):
        """Delete the chart from the worksheet."""
    
    def to_pdf(self, path: str):
        """
        Export chart to PDF file.
        
        Args:
            path (str): Output file path for PDF.
        """
    
    def to_png(self, path: str):
        """
        Export chart to PNG image file.
        
        Args:
            path (str): Output file path for PNG image.
        """
    
    @property
    def name(self) -> str:
        """Chart name/title."""
    
    @name.setter
    def name(self, value: str): ...
    
    @property
    def chart_type(self):
        """Get/set chart type using Excel chart type constants."""
    
    @chart_type.setter
    def chart_type(self, value): ...
    
    @property
    def source_data(self) -> Range:
        """Range containing the chart's source data."""
    
    @source_data.setter
    def source_data(self, value: Range): ...
    
    @property
    def api(self):
        """Access to native Excel chart object for advanced manipulation."""

Picture Objects

Handles images in Excel worksheets, supporting various image formats and matplotlib figure integration.

class Picture:
    """Represents an image/picture in Excel worksheet."""
    
    def delete(self):
        """Delete the picture from the worksheet."""
    
    def update(self, image):
        """
        Update picture with new image data.
        
        Args:
            image: New image data. Supports file paths, matplotlib figures,
                  PIL images, or image data.
        """
    
    @property
    def name(self) -> str:
        """Picture name/identifier."""
    
    @property
    def left(self) -> float:
        """Left position in points from worksheet origin."""
    
    @left.setter
    def left(self, value: float): ...
    
    @property
    def top(self) -> float:
        """Top position in points from worksheet origin."""
    
    @top.setter
    def top(self, value: float): ...
    
    @property
    def width(self) -> float:
        """Picture width in points."""
    
    @width.setter
    def width(self, value: float): ...
    
    @property
    def height(self) -> float:
        """Picture height in points."""
    
    @height.setter
    def height(self, value: float): ...
    
    @property
    def api(self):
        """Access to native Excel picture object."""

Shape Objects

Represents Excel shapes and drawing objects, including text boxes, lines, and geometric shapes.

class Shape:
    """Represents Excel shape/drawing object."""
    
    def delete(self):
        """Delete the shape from the worksheet."""
    
    def duplicate(self):
        """
        Create a duplicate of this shape.
        
        Returns:
            Shape: New shape object that is a copy of this shape.
        """
    
    @property
    def name(self) -> str:
        """Shape name/identifier."""
    
    @property
    def type(self):
        """Shape type (text box, rectangle, line, etc.)."""
    
    @property
    def left(self) -> float:
        """Left position in points from worksheet origin."""
    
    @left.setter
    def left(self, value: float): ...
    
    @property
    def top(self) -> float:
        """Top position in points from worksheet origin."""
    
    @top.setter  
    def top(self, value: float): ...
    
    @property
    def width(self) -> float:
        """Shape width in points."""
    
    @width.setter
    def width(self, value: float): ...
    
    @property
    def height(self) -> float:
        """Shape height in points."""
    
    @height.setter
    def height(self, value: float): ...
    
    @property
    def api(self):
        """Access to native Excel shape object for advanced manipulation."""

Usage examples:

import xlwings as xw
import matplotlib.pyplot as plt
from xlwings.constants import ChartType
import numpy as np

wb = xw.books.add()
ws = wb.sheets[0]

# Create sample data and chart
ws.range('A1:B6').value = [
    ['Month', 'Sales'],
    ['Jan', 100], ['Feb', 120], ['Mar', 140], 
    ['Apr', 110], ['May', 160]
]

chart = ws.charts.add()
chart.source_data = ws.range('A1:B6')
chart.chart_type = ChartType.xlColumnClustered
chart.name = 'Monthly Sales'

# Add matplotlib figure
fig, ax = plt.subplots()
x = np.linspace(0, 10, 100)
y = np.sin(x)
ax.plot(x, y)
ax.set_title('Sine Wave')

pic = ws.pictures.add(fig, name='SinePlot',
                     left=ws.range('E1').left,
                     top=ws.range('E1').top)

# Export visualizations
chart.to_png('/path/to/sales_chart.png')
chart.to_pdf('/path/to/sales_chart.pdf')

Types

# Chart and visualization types
ChartObject = Chart
PictureObject = Picture  
ShapeObject = Shape

# Collection types
Charts = Collection[Chart]
Pictures = Collection[Picture]
Shapes = Collection[Shape]

# Position and size types
Position = tuple[float, float]  # (left, top)
Size = tuple[float, float]      # (width, height)

# Format types
ImageFormat = Literal['png', 'jpg', 'jpeg', 'bmp', 'gif']
ExportFormat = Literal['pdf', 'png', 'jpg']
ChartTypeConstant = int  # Excel chart type constants

Install with Tessl CLI

npx tessl i tessl/pypi-xlwings

docs

charts-visualization.md

cli.md

collections-functions.md

constants.md

conversion.md

core-objects.md

index.md

pro-features.md

udfs.md

utilities.md

tile.json