strings.py 1.1 KB

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