_writer.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. from __future__ import absolute_import
  2. # Copyright (c) 2010-2019 openpyxl
  3. """Write the workbook global settings to the archive."""
  4. from copy import copy
  5. from openpyxl.utils import absolute_coordinate, quote_sheetname
  6. from openpyxl.xml.constants import (
  7. ARC_APP,
  8. ARC_CORE,
  9. ARC_WORKBOOK,
  10. PKG_REL_NS,
  11. CUSTOMUI_NS,
  12. ARC_ROOT_RELS,
  13. )
  14. from openpyxl.xml.functions import tostring, fromstring
  15. from openpyxl.packaging.relationship import Relationship, RelationshipList
  16. from openpyxl.workbook.defined_name import DefinedName
  17. from openpyxl.workbook.external_reference import ExternalReference
  18. from openpyxl.packaging.workbook import ChildSheet, WorkbookPackage, PivotCache
  19. from openpyxl.workbook.properties import WorkbookProperties
  20. from openpyxl.utils.datetime import CALENDAR_MAC_1904
  21. def get_active_sheet(wb):
  22. """
  23. Return the index of the active sheet.
  24. If the sheet set to active is hidden return the next visible sheet or None
  25. """
  26. visible_sheets = [idx for idx, sheet in enumerate(wb._sheets) if sheet.sheet_state == "visible"]
  27. if not visible_sheets:
  28. raise IndexError("At least one sheet must be visible")
  29. idx = wb._active_sheet_index
  30. sheet = wb.active
  31. if sheet and sheet.sheet_state == "visible":
  32. return idx
  33. for idx in visible_sheets[idx:]:
  34. wb.active = idx
  35. return idx
  36. return None
  37. class WorkbookWriter:
  38. def __init__(self, wb):
  39. self.wb = wb
  40. self.rels = RelationshipList()
  41. self.package = WorkbookPackage()
  42. self.package.workbookProtection = wb.security
  43. self.package.calcPr = wb.calculation
  44. def write_properties(self):
  45. props = WorkbookProperties() # needs a mapping to the workbook for preservation
  46. if self.wb.code_name is not None:
  47. props.codeName = self.wb.code_name
  48. if self.wb.excel_base_date == CALENDAR_MAC_1904:
  49. props.date1904 = True
  50. self.package.workbookPr = props
  51. def write_worksheets(self):
  52. for idx, sheet in enumerate(self.wb._sheets, 1):
  53. sheet_node = ChildSheet(name=sheet.title, sheetId=idx, id="rId{0}".format(idx))
  54. rel = Relationship(type=sheet._rel_type, Target=sheet.path)
  55. self.rels.append(rel)
  56. if not sheet.sheet_state == 'visible':
  57. if len(self.wb._sheets) == 1:
  58. raise ValueError("The only worksheet of a workbook cannot be hidden")
  59. sheet_node.state = sheet.sheet_state
  60. self.package.sheets.append(sheet_node)
  61. def write_refs(self):
  62. for link in self.wb._external_links:
  63. # need to match a counter with a workbook's relations
  64. rId = len(self.wb.rels) + 1
  65. rel = Relationship(type=link._rel_type, Target=link.path)
  66. self.rels.append(rel)
  67. ext = ExternalReference(id=rel.id)
  68. self.package.externalReferences.append(ext)
  69. def write_names(self):
  70. defined_names = copy(self.wb.defined_names)
  71. # Defined names -> autoFilter
  72. for idx, sheet in enumerate(self.wb.worksheets):
  73. auto_filter = sheet.auto_filter.ref
  74. if auto_filter:
  75. name = DefinedName(name='_FilterDatabase', localSheetId=idx, hidden=True)
  76. name.value = u"{0}!{1}".format(quote_sheetname(sheet.title),
  77. absolute_coordinate(auto_filter)
  78. )
  79. defined_names.append(name)
  80. # print titles
  81. if sheet.print_titles:
  82. name = DefinedName(name="Print_Titles", localSheetId=idx)
  83. name.value = ",".join([u"{0}!{1}".format(quote_sheetname(sheet.title), r)
  84. for r in sheet.print_titles.split(",")])
  85. defined_names.append(name)
  86. # print areas
  87. if sheet.print_area:
  88. name = DefinedName(name="Print_Area", localSheetId=idx)
  89. name.value = ",".join([u"{0}!{1}".format(quote_sheetname(sheet.title), r)
  90. for r in sheet.print_area])
  91. defined_names.append(name)
  92. self.package.definedNames = defined_names
  93. def write_pivots(self):
  94. pivot_caches = set()
  95. for pivot in self.wb._pivots:
  96. if pivot.cache not in pivot_caches:
  97. pivot_caches.add(pivot.cache)
  98. c = PivotCache(cacheId=pivot.cacheId)
  99. self.package.pivotCaches.append(c)
  100. rel = Relationship(Type=pivot.cache.rel_type, Target=pivot.cache.path)
  101. self.rels.append(rel)
  102. c.id = rel.id
  103. #self.wb._pivots = [] # reset
  104. def write_views(self):
  105. active = get_active_sheet(self.wb)
  106. if self.wb.views:
  107. self.wb.views[0].activeTab = active
  108. self.package.bookViews = self.wb.views
  109. def write(self):
  110. """Write the core workbook xml."""
  111. self.write_properties()
  112. self.write_worksheets()
  113. self.write_names()
  114. self.write_pivots()
  115. self.write_views()
  116. self.write_refs()
  117. return tostring(self.package.to_tree())
  118. def write_rels(self):
  119. """Write the workbook relationships xml."""
  120. styles = Relationship(type='styles', Target='styles.xml')
  121. self.rels.append(styles)
  122. theme = Relationship(type='theme', Target='theme/theme1.xml')
  123. self.rels.append(theme)
  124. if self.wb.vba_archive:
  125. vba = Relationship(type='', Target='vbaProject.bin')
  126. vba.Type ='http://schemas.microsoft.com/office/2006/relationships/vbaProject'
  127. self.rels.append(vba)
  128. return tostring(self.rels.to_tree())
  129. def write_root_rels(self):
  130. """Write the package relationships"""
  131. rels = RelationshipList()
  132. rel = Relationship(type="officeDocument", Target=ARC_WORKBOOK)
  133. rels.append(rel)
  134. rel = Relationship(Type="%s/metadata/core-properties" % PKG_REL_NS, Target=ARC_CORE)
  135. rels.append(rel)
  136. rel = Relationship(type="extended-properties", Target=ARC_APP)
  137. rels.append(rel)
  138. if self.wb.vba_archive is not None:
  139. # See if there was a customUI relation and reuse it
  140. xml = fromstring(self.wb.vba_archive.read(ARC_ROOT_RELS))
  141. root_rels = RelationshipList.from_tree(xml)
  142. for rel in root_rels.find(CUSTOMUI_NS):
  143. rels.append(rel)
  144. return tostring(rels.to_tree())