Skip to main content
Ctrl+K

XlsxWriter

  • Contents
    • Introduction
    • Getting Started with XlsxWriter
    • Tutorial 1: Create a simple XLSX file
    • Tutorial 2: Adding formatting to the XLSX File
    • Tutorial 3: Writing different types of data to the XLSX File
    • The Workbook Class
    • The Worksheet Class
    • The Worksheet Class (Page Setup)
    • The Format Class
    • The Color Class
    • The Chart Class
    • The Chartsheet Class
    • The Exceptions Class
    • Utility and Helper Functions
    • Working with Cell Notation
    • Working with and Writing Data
    • Working with Formulas
    • Working with Dates and Time
    • Working with Colors
    • Working with Charts
    • Working with Object Positioning
    • Working with Autofilters
    • Working with Data Validation
    • Working with Conditional Formatting
    • Working with Worksheet Tables
    • Working with Textboxes
    • Working with Sparklines
    • Working with Cell Comments
    • Working with Outlines and Grouping
    • Working with Memory and Performance
    • Working with VBA Macros
    • Working with Pandas and XlsxWriter
    • Working with Polars and XlsxWriter
    • Examples
    • Chart Examples
    • Pandas with XlsxWriter Examples
    • Polars with XlsxWriter Examples
    • Alternative modules for handling Excel files
    • Libraries that use or enhance XlsxWriter
    • Known Issues and Bugs
    • Frequently Asked Questions
    • Changes in XlsxWriter
    • Author
    • License
  • Contents
  • Introduction
  • Getting Started with XlsxWriter
  • Tutorial 1: Create a simple XLSX file
  • Tutorial 2: Adding formatting to the XLSX File
  • Tutorial 3: Writing different types of data to the XLSX File
  • The Workbook Class
  • The Worksheet Class
  • The Worksheet Class (Page Setup)
  • The Format Class
  • The Color Class
  • The Chart Class
  • The Chartsheet Class
  • The Exceptions Class
  • Utility and Helper Functions
  • Working with Cell Notation
  • Working with and Writing Data
  • Working with Formulas
  • Working with Dates and Time
  • Working with Colors
  • Working with Charts
  • Working with Object Positioning
  • Working with Autofilters
  • Working with Data Validation
  • Working with Conditional Formatting
  • Working with Worksheet Tables
  • Working with Textboxes
  • Working with Sparklines
  • Working with Cell Comments
  • Working with Outlines and Grouping
  • Working with Memory and Performance
  • Working with VBA Macros
  • Working with Pandas and XlsxWriter
  • Working with Polars and XlsxWriter
  • Examples
  • Chart Examples
  • Pandas with XlsxWriter Examples
  • Polars with XlsxWriter Examples
  • Alternative modules for handling Excel files
  • Libraries that use or enhance XlsxWriter
  • Known Issues and Bugs
  • Frequently Asked Questions
  • Changes in XlsxWriter
  • Author
  • License

Section Navigation

  • Example: Hello World
  • Example: Simple Feature Demonstration
  • Example: Catch exception on closing
  • Example: Dates and Times in Excel
  • Example: Adding hyperlinks
  • Example: Array formulas
  • Example: Dynamic array formulas
  • Example: Applying Autofilters
  • Example: Data Validation and Drop Down Lists
  • Example: Conditional Formatting
  • Example: Defined names/Named ranges
  • Example: Merging Cells
  • Example: Autofitting columns
  • Example: Autofitting columns manually
  • Example: Writing “Rich” strings with multiple formats
  • Example: Merging Cells with a Rich String
  • Example: Inserting images into a worksheet
  • Example: Inserting images from a URL or byte stream into a worksheet
  • Example: Embedding images into a worksheet
  • Example: Left to Right worksheets and text
  • Example: Simple Django class
  • Example: Simple HTTP Server
  • Example: Adding Headers and Footers to Worksheets
  • Example: Freeze Panes and Split Panes
  • Example: Worksheet Tables
  • Example: Inserting a checkbox in a Worksheet
  • Example: Writing User Defined Types (1)
  • Example: Writing User Defined Types (2)
  • Example: Writing User Defined types (3)
  • Example: Ignoring Worksheet errors and warnings
  • Example: Sparklines (Simple)
  • Example: Sparklines (Advanced)
  • Example: Adding Cell Comments to Worksheets (Simple)
  • Example: Adding Cell Comments to Worksheets (Advanced)
  • Example: Insert Textboxes into a Worksheet
  • Example: Outline and Grouping
  • Example: Collapsed Outline and Grouping
  • Example: Setting Document Properties
  • Example: Setting the Sensitivity Label for a Document
  • Example: Simple Unicode with Python 3
  • Example: Unicode - Polish in UTF-8
  • Example: Unicode - Shift JIS
  • Example: Setting a Worksheet Watermark
  • Example: Setting the Worksheet Background
  • Example: Setting Worksheet Tab Colors
  • Example: Diagonal borders in cells
  • Example: Enabling Cell protection in Worksheets
  • Example: Hiding Worksheets
  • Example: Hiding Rows and Columns
  • Example: Example of subclassing the Workbook and Worksheet classes
  • Example: Advanced example of subclassing
  • Example: Adding a VBA macro to a Workbook
  • Example: Excel 365 LAMBDA() function
  • Contents
  • Examples
  • Example: Adding hyperlinks

Example: Adding hyperlinks#

This program is an example of writing hyperlinks to a worksheet. See the write_url() method for more details.

_images/hyperlink.png
###############################################################################
#
# Example of how to use the XlsxWriter module to write hyperlinks
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2013-2025, John McNamara, jmcnamara@cpan.org
#
import xlsxwriter

# Create a new workbook and add a worksheet
workbook = xlsxwriter.Workbook("hyperlink.xlsx")
worksheet = workbook.add_worksheet("Hyperlinks")

# Format the first column
worksheet.set_column("A:A", 30)

# Add a sample alternative link format.
red_format = workbook.add_format(
    {
        "font_color": "red",
        "bold": 1,
        "underline": 1,
        "font_size": 12,
    }
)

# Write some hyperlinks
worksheet.write_url("A1", "http://www.python.org/")  # Implicit format.
worksheet.write_url("A3", "http://www.python.org/", string="Python Home")
worksheet.write_url("A5", "http://www.python.org/", tip="Click here")
worksheet.write_url("A7", "http://www.python.org/", red_format)
worksheet.write_url("A9", "mailto:jmcnamara@cpan.org", string="Mail me")

# Write a URL that isn't a hyperlink
worksheet.write_string("A11", "http://www.python.org/")

workbook.close()

previous

Example: Dates and Times in Excel

next

Example: Array formulas

© Copyright 2013-2025, John McNamara.

Created using Sphinx 8.2.3.

Built with the PyData Sphinx Theme 0.16.1.