1# Copyright (c) 2010-2024 openpyxl
2
3from openpyxl.cell.text import Text
4
5from openpyxl.xml.functions import iterparse
6from openpyxl.xml.constants import SHEET_MAIN_NS
7from openpyxl.cell.rich_text import CellRichText
8
9
10def read_string_table(xml_source):
11 """Read in all shared strings in the table"""
12
13 strings = []
14 STRING_TAG = '{%s}si' % SHEET_MAIN_NS
15
16 for _, node in iterparse(xml_source):
17 if node.tag == STRING_TAG:
18 text = Text.from_tree(node).content
19 text = text.replace('x005F_', '')
20 node.clear()
21
22 strings.append(text)
23
24 return strings
25
26
27def read_rich_text(xml_source):
28 """Read in all shared strings in the table"""
29
30 strings = []
31 STRING_TAG = '{%s}si' % SHEET_MAIN_NS
32
33 for _, node in iterparse(xml_source):
34 if node.tag == STRING_TAG:
35 text = CellRichText.from_tree(node)
36 if len(text) == 0:
37 text = ''
38 elif len(text) == 1 and isinstance(text[0], str):
39 text = text[0]
40 node.clear()
41
42 strings.append(text)
43
44 return strings