English 中文(简体)
Python XlsxWriter - Hyperlinks
  • 时间:2024-12-22

Python XlsxWriter - Hyperpnks


Previous Page Next Page  

A hyperpnk is a string, which when cpcked, takes the user to some other location, such as a URL, another worksheet in the same workbook or another workbook on the computer. Worksheet class provides write_url() method for the purpose. Hyperpnks can also be placed inside a textbox with the use of url property.

First, let us learn about write_url() method. In addition to the Cell location, it needs the URL string to be directed to.


import xlsxwriter

workbook = xlsxwriter.Workbook( hello.xlsx )
worksheet = workbook.add_worksheet()
worksheet.write_url( A1 ,  https://www.tutorialspoint.com/index.htm )

workbook.close()

This method has a few optional parameters. One is a Format object to configure the font, color properties of the URL to be displayed. We can also specify a tool tip string and a display text foe the URL. When the text is not given, the URL itself appears in the cell.

Example

Different types of URLs supported are http://, https://, ftp:// and mailto:. In the example below, we use these URLs.


import xlsxwriter

workbook = xlsxwriter.Workbook( hello.xlsx )
worksheet = workbook.add_worksheet()

worksheet.write_url( A1 ,  https://www.tutorialspoint.com/index.htm )
worksheet.write_url( A3 ,  http://localhost:8080 )
worksheet.write_url( A5 ,  ftp://www.python.org )
worksheet.write_url( A7 ,  mailto:dummy@abc.com )

workbook.close()

Output

Run the above code and open the hello.xlsx file using Excel.

HyperLink1

Example

We can also insert hyperpnk to either another workskeet in the same workbook, or another workbook. This is done by prefixing with internal: or external: the local URIs.


import xlsxwriter

workbook = xlsxwriter.Workbook( hello.xlsx )
worksheet = workbook.add_worksheet()

worksheet.write_url( A1 ,  internal:Sheet2!A1 , string="Link to sheet2", tip="Cpck here")
worksheet.write_url( A4 , "external:c:/test/testpnk.xlsx", string="Link to other workbook")
   
workbook.close()

Output

Note that the string and tip parameters are given as an alternative text to the pnk and tool tip. The output of the above program is as given below −

HyperLink2 Advertisements