-
-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xmlwriter: fix issue with control chartacter in data elements
Issue #978
- Loading branch information
Showing
5 changed files
with
85 additions
and
39 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 |
---|---|---|
|
@@ -6,17 +6,10 @@ | |
# Copyright 2013-2023, John McNamara, [email protected] | ||
# | ||
|
||
# Standard packages. | ||
import re | ||
|
||
# Package imports. | ||
from . import xmlwriter | ||
from .utility import preserve_whitespace | ||
|
||
# Compile performance critical regular expressions. | ||
re_control_chars_1 = re.compile("(_x[0-9a-fA-F]{4}_)") | ||
re_control_chars_2 = re.compile(r"([\x00-\x08\x0b-\x1f])") | ||
|
||
|
||
class SharedStrings(xmlwriter.XMLwriter): | ||
""" | ||
|
@@ -92,22 +85,8 @@ def _write_si(self, string): | |
# Write the <si> element. | ||
attributes = [] | ||
|
||
# Excel escapes control characters with _xHHHH_ and also escapes any | ||
# literal strings of that type by encoding the leading underscore. | ||
# So "\0" -> _x0000_ and "_x0000_" -> _x005F_x0000_. | ||
# The following substitutions deal with those cases. | ||
|
||
# Escape the escape. | ||
string = re_control_chars_1.sub(r"_x005F\1", string) | ||
|
||
# Convert control character to the _xHHHH_ escape. | ||
string = re_control_chars_2.sub( | ||
lambda match: "_x%04X_" % ord(match.group(1)), string | ||
) | ||
|
||
# Escapes non characters in strings. | ||
string = string.replace("\uFFFE", "_xFFFE_") | ||
string = string.replace("\uFFFF", "_xFFFF_") | ||
# Convert control character to a _xHHHH_ escape. | ||
string = self._escape_control_characters(string) | ||
|
||
# Add attribute to preserve leading or trailing whitespace. | ||
if preserve_whitespace(string): | ||
|
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,50 @@ | ||
############################################################################### | ||
# | ||
# Tests for XlsxWriter. | ||
# | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
# Copyright (c), 2013-2023, John McNamara, [email protected] | ||
# | ||
|
||
from ..excel_comparison_test import ExcelComparisonTest | ||
from ...workbook import Workbook | ||
|
||
|
||
class TestCompareXLSXFiles(ExcelComparisonTest): | ||
""" | ||
Test file created by XlsxWriter against a file created by Excel. | ||
""" | ||
|
||
def setUp(self): | ||
self.set_filename("escapes09.xlsx") | ||
|
||
def test_create_file(self): | ||
"""Test the creation of a simple XlsxWriter file.""" | ||
|
||
workbook = Workbook(self.got_filename) | ||
|
||
worksheet = workbook.add_worksheet() | ||
chart = workbook.add_chart({"type": "line"}) | ||
|
||
chart.axis_ids = [52721920, 53133312] | ||
|
||
worksheet.write(0, 0, "Data\x1b[32m1") | ||
worksheet.write(1, 0, "Data\x1b[32m2") | ||
worksheet.write(2, 0, "Data\x1b[32m3") | ||
worksheet.write(3, 0, "Data\x1b[32m4") | ||
|
||
worksheet.write(0, 1, 10) | ||
worksheet.write(1, 1, 20) | ||
worksheet.write(2, 1, 10) | ||
worksheet.write(3, 1, 30) | ||
|
||
chart.add_series( | ||
{"categories": "=Sheet1!$A$1:$A$4", "values": "=Sheet1!$B$1:$B$4"} | ||
) | ||
|
||
worksheet.insert_chart("E9", chart) | ||
|
||
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
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