XlsxWriter
  • Contents
      • 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 Chart Class
      • The Chartsheet Class
      • The Exceptions Class
      • 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 Python Pandas and XlsxWriter
      • Examples
      • Chart Examples
      • Pandas with XlsxWriter Examples
      • Alternative modules for handling Excel files
      • Libraries that use or enhance XlsxWriter
      • Known Issues and Bugs
      • Reporting Bugs
      • Frequently Asked Questions
      • Changes in XlsxWriter
      • Author
      • License
  • Page
      • Example: Adding hyperlinks
  • « Example: Date...
  • Example: Arra... »

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 2013-2023, 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()

Back to top

© Copyright 2013-2023, John McNamara.
Created using Sphinx 1.8.6.