1############################################################################## 2# 3# An example of adding support for user defined types to the XlsxWriter write() 4# method. 5# 6# Copyright 2013-2021, John McNamara, jmcnamara@cpan.org 7# 8import xlsxwriter 9import uuid 10 11# Create a function that will behave like a worksheet write() method. 12# 13# This function takes a UUID and writes it as as string. It should take the 14# parameters shown below and return the return value from the called worksheet 15# write_*() method. In this case it changes the UUID to a string and calls 16# write_string() to write it. 17# 18def write_uuid(worksheet, row, col, token, format=None): 19 return worksheet.write_string(row, col, str(token), format) 20 21# Set up the workbook as usual. 22workbook = xlsxwriter.Workbook('user_types1.xlsx') 23worksheet = workbook.add_worksheet() 24 25# Make the first column wider for clarity. 26worksheet.set_column('A:A', 40) 27 28# Add the write() handler/callback to the worksheet. 29worksheet.add_write_handler(uuid.UUID, write_uuid) 30 31# Create a UUID. 32my_uuid = uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') 33 34# Write the UUID. This would raise a TypeError without the handler. 35worksheet.write('A1', my_uuid) 36 37workbook.close() 38