-
-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for adding user defined types and callbacks to write additional data types. Issue #631
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
############################################################################## | ||
# | ||
# An example of adding support for user defined types to the XlsxWriter write() | ||
# method. | ||
# | ||
# Copyright 2013-2019, John McNamara, jmcnamara@cpan.org | ||
# | ||
import xlsxwriter | ||
import uuid | ||
|
||
# Create a function that will behave like a worksheet write() method. | ||
# | ||
# This function takes a UUID and writes it as as string. It should take the | ||
# parameters shown below and return the return value from the called worksheet | ||
# write_*() method. In this case it changes the UUID to a string and calls | ||
# write_string() to write it. | ||
# | ||
def write_uuid(worksheet, row, col, token, format=None): | ||
return worksheet.write_string(row, col, str(token), format) | ||
|
||
# Set up the workbook as usual. | ||
workbook = xlsxwriter.Workbook('user_type1.xlsx') | ||
worksheet = workbook.add_worksheet() | ||
|
||
# Make the first column wider for clarity. | ||
worksheet.set_column('A:A', 40) | ||
|
||
# Add the write() handler/callback to the worksheet. | ||
worksheet.add_write_handler(uuid.UUID, write_uuid) | ||
|
||
# Create a UUID. | ||
my_uuid = uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') | ||
|
||
# Write the UUID. This would raise a TypeError without the handler. | ||
worksheet.write('A1', my_uuid) | ||
|
||
workbook.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
############################################################################### | ||
# | ||
# Tests for XlsxWriter. | ||
# | ||
# Copyright (c), 2013-2019, John McNamara, jmcnamara@cpan.org | ||
# | ||
|
||
from ..excel_comparsion_test import ExcelComparisonTest | ||
from ...workbook import Workbook | ||
import uuid | ||
|
||
|
||
def write_uuid(worksheet, row, col, token, format=None): | ||
return worksheet.write_string(row, col, str(token), format) | ||
|
||
|
||
class TestCompareXLSXFiles(ExcelComparisonTest): | ||
""" | ||
Test file created by XlsxWriter against a file created by Excel. | ||
""" | ||
|
||
def setUp(self): | ||
|
||
self.set_filename('types10.xlsx') | ||
|
||
def test_write_user_type(self): | ||
"""Test writing numbers as text.""" | ||
|
||
workbook = Workbook(self.got_filename) | ||
worksheet = workbook.add_worksheet() | ||
|
||
worksheet.add_write_handler(uuid.UUID, write_uuid) | ||
my_uuid = uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') | ||
|
||
worksheet.write('A1', my_uuid) | ||
|
||
workbook.close() | ||
|
||
self.assertExcelEqual() |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters