CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-xlsxwriter

A Python module for creating Excel XLSX files.

Overview
Eval results
Files

data-writing.mddocs/

Data Writing and Cell Operations

Comprehensive data writing capabilities for Excel worksheets including support for all Excel data types, formulas, hyperlinks, rich text, and specialized writing methods for rows, columns, and cell ranges.

Capabilities

Generic Writing Methods

Universal data writing methods that automatically detect data types.

def write(self, row, col, *args):
    """
    Write data to a worksheet cell with automatic type detection.
    
    Args:
        row (int): Cell row (0-indexed)
        col (int): Cell column (0-indexed)
        *args: Data and optional format. Can be:
            - (data,) - Write data with default formatting
            - (data, cell_format) - Write data with specific format
            - (data, cell_format, value) - For formulas with cached value
            
    The method automatically detects the data type and calls the
    appropriate write_* method.
    """

def add_write_handler(self, user_type, user_function):
    """
    Add a custom handler for writing user-defined data types.
    
    Args:
        user_type (type): The user-defined data type
        user_function (callable): Function to handle writing this type
    """

String and Text Writing

Methods for writing text data including strings and rich formatted text.

def write_string(self, row, col, string, cell_format=None):
    """
    Write a string to a worksheet cell.
    
    Args:
        row (int): Cell row (0-indexed)
        col (int): Cell column (0-indexed)
        string (str): String data to write
        cell_format (Format, optional): Cell formatting to apply
    """

def write_rich_string(self, row, col, *args):
    """
    Write a rich text string with multiple formats.
    
    Args:
        row (int): Cell row (0-indexed)
        col (int): Cell column (0-indexed)
        *args: Alternating string and format arguments:
            - 'string1', format1, 'string2', format2, ...
            - Final format argument is optional and applies to the cell
            
    Example:
        write_rich_string(0, 0, 'Bold', bold_format, ' and normal text')
    """

Numeric Data Writing

Methods for writing numeric data including integers, floats, and special numeric formats.

def write_number(self, row, col, number, cell_format=None):
    """
    Write a numeric value to a worksheet cell.
    
    Args:
        row (int): Cell row (0-indexed)
        col (int): Cell column (0-indexed)
        number (int or float): Numeric data to write
        cell_format (Format, optional): Cell formatting to apply
    """

def write_blank(self, row, col, blank, cell_format=None):
    """
    Write a blank cell with optional formatting.
    
    Args:
        row (int): Cell row (0-indexed)
        col (int): Cell column (0-indexed)
        blank: Ignored (for compatibility)
        cell_format (Format, optional): Cell formatting to apply
    """

Formula Writing

Methods for writing Excel formulas including standard, array, and dynamic formulas.

def write_formula(self, row, col, formula, cell_format=None, value=0):
    """
    Write an Excel formula to a worksheet cell.
    
    Args:
        row (int): Cell row (0-indexed)
        col (int): Cell column (0-indexed)
        formula (str): Excel formula string (without leading =)
        cell_format (Format, optional): Cell formatting to apply
        value (numeric, optional): Cached result value for the formula
    """

def write_array_formula(self, first_row, first_col, last_row, last_col, 
                       formula, cell_format=None, value=0):
    """
    Write an array formula to a range of cells.
    
    Args:
        first_row (int): First row of the range (0-indexed)
        first_col (int): First column of the range (0-indexed)
        last_row (int): Last row of the range (0-indexed)
        last_col (int): Last column of the range (0-indexed)
        formula (str): Array formula string (without leading =)
        cell_format (Format, optional): Cell formatting to apply
        value (numeric, optional): Cached result value
    """

def write_dynamic_array_formula(self, row, col, formula, cell_format=None, value=0):
    """
    Write a dynamic array formula (Excel 365).
    
    Args:
        row (int): Cell row (0-indexed)
        col (int): Cell column (0-indexed)
        formula (str): Dynamic array formula string (without leading =)
        cell_format (Format, optional): Cell formatting to apply
        value (numeric, optional): Cached result value
    """

Date and Time Writing

Methods for writing date and time data with proper Excel serialization.

def write_datetime(self, row, col, date, cell_format=None):
    """
    Write a datetime object to a worksheet cell.
    
    Args:
        row (int): Cell row (0-indexed)
        col (int): Cell column (0-indexed)
        date (datetime): Python datetime object
        cell_format (Format, optional): Cell formatting (should include date format)
        
    Note: The cell_format should include a date/time number format
    """

Boolean Data Writing

Methods for writing boolean values and checkbox controls.

def write_boolean(self, row, col, boolean, cell_format=None):
    """
    Write a boolean value to a worksheet cell.
    
    Args:
        row (int): Cell row (0-indexed)
        col (int): Cell column (0-indexed)
        boolean (bool): Boolean value (True/False)
        cell_format (Format, optional): Cell formatting to apply
    """

URL and Hyperlink Writing

Methods for writing hyperlinks and URLs with optional display text and tooltips.

def write_url(self, row, col, url, cell_format=None, string=None, tip=None):
    """
    Write a hyperlink to a worksheet cell.
    
    Args:
        row (int): Cell row (0-indexed)
        col (int): Cell column (0-indexed)
        url (str): The URL to link to (http://, https://, ftp://, mailto:, 
                  internal:, external:)
        cell_format (Format, optional): Cell formatting (defaults to hyperlink style)
        string (str, optional): Display text for the hyperlink
        tip (str, optional): Tooltip text for the hyperlink
    """

Bulk Data Writing

Methods for writing arrays of data to rows and columns efficiently.

def write_row(self, row, col, data, cell_format=None):
    """
    Write a row of data starting from the given cell.
    
    Args:
        row (int): Starting row (0-indexed)
        col (int): Starting column (0-indexed)
        data (list): List of data items to write
        cell_format (Format, optional): Format to apply to all cells
    """

def write_column(self, row, col, data, cell_format=None):
    """
    Write a column of data starting from the given cell.
    
    Args:
        row (int): Starting row (0-indexed)
        col (int): Starting column (0-indexed)
        data (list): List of data items to write
        cell_format (Format, optional): Format to apply to all cells
    """

Usage Examples

Basic Data Writing

import xlsxwriter
from datetime import datetime

workbook = xlsxwriter.Workbook('data.xlsx')
worksheet = workbook.add_worksheet()

# Write different data types
worksheet.write_string(0, 0, 'Hello World')
worksheet.write_number(1, 0, 123.45)
worksheet.write_boolean(2, 0, True)
worksheet.write_datetime(3, 0, datetime.now())

# Generic write method (auto-detects type)
worksheet.write(4, 0, 'Auto-detected string')
worksheet.write(5, 0, 42)

workbook.close()

Formula Writing

# Simple formulas
worksheet.write_formula(0, 0, 'SUM(B1:B10)')
worksheet.write_formula(1, 0, 'AVERAGE(B1:B10)', currency_format)

# Array formula
worksheet.write_array_formula(0, 0, 2, 0, 'TRANSPOSE(B1:D1)')

# Dynamic array formula (Excel 365)
worksheet.write_dynamic_array_formula(0, 0, 'SEQUENCE(5,1)')

Rich Text Formatting

# Create formats
bold = workbook.add_format({'bold': True})
italic = workbook.add_format({'italic': True})
red = workbook.add_format({'font_color': 'red'})

# Write rich text
worksheet.write_rich_string(0, 0,
    'This is ', bold, 'bold', ' and this is ', italic, 'italic',
    ' and this is ', red, 'red text')

Hyperlinks

# External URLs
worksheet.write_url(0, 0, 'https://www.python.org')
worksheet.write_url(1, 0, 'https://www.python.org', None, 'Python Website')

# Email links
worksheet.write_url(2, 0, 'mailto:john@example.com', None, 'Send Email')

# Internal links
worksheet.write_url(3, 0, 'internal:Sheet2!A1', None, 'Go to Sheet2')

# External file links
worksheet.write_url(4, 0, 'external:other.xlsx#Sheet1!A1', None, 'External File')

Bulk Data Operations

# Write row of data
headers = ['Name', 'Age', 'City', 'Salary']
worksheet.write_row(0, 0, headers, header_format)

# Write column of data
names = ['Alice', 'Bob', 'Charlie', 'Diana']
worksheet.write_column(1, 0, names)

# Write 2D data
data = [
    ['Alice', 25, 'New York', 50000],
    ['Bob', 30, 'London', 60000],
    ['Charlie', 35, 'Tokyo', 70000]
]

for row_num, row_data in enumerate(data, 1):
    worksheet.write_row(row_num, 0, row_data)

Custom Data Types

# Define custom data type handler
from decimal import Decimal

def write_decimal(worksheet, row, col, decimal_val, format=None):
    return worksheet.write_number(row, col, float(decimal_val), format)

# Register the handler
worksheet.add_write_handler(Decimal, write_decimal)

# Now Decimal objects are handled automatically
worksheet.write(0, 0, Decimal('123.45'))

Install with Tessl CLI

npx tessl i tessl/pypi-xlsxwriter

docs

advanced-features.md

charts.md

data-writing.md

formatting.md

index.md

page-setup.md

utilities.md

workbook.md

tile.json