/usr/lib/python2.7/dist-packages/openpyxl/writer/workbook.py is in python-openpyxl 2.3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | from __future__ import absolute_import
# Copyright (c) 2010-2015 openpyxl
"""Write the workbook global settings to the archive."""
# package imports
from openpyxl import LXML
from openpyxl.compat import safe_string
from openpyxl.utils import absolute_coordinate
from openpyxl.xml.functions import Element, SubElement
from openpyxl.xml.constants import (
ARC_CORE,
ARC_WORKBOOK,
ARC_APP,
COREPROPS_NS,
VTYPES_NS,
XPROPS_NS,
DCORE_NS,
DCTERMS_NS,
DCTERMS_PREFIX,
XSI_NS,
SHEET_MAIN_NS,
CONTYPES_NS,
PKG_REL_NS,
CUSTOMUI_NS,
REL_NS,
ARC_CUSTOM_UI,
ARC_ROOT_RELS,
)
from openpyxl.xml.functions import tostring, fromstring
from openpyxl.utils.datetime import datetime_to_W3CDTF
from openpyxl.worksheet import Worksheet
from openpyxl.chartsheet import Chartsheet
from openpyxl.packaging.relationship import Relationship, RelationshipList
from openpyxl.workbook.properties import write_properties
def write_properties_app(workbook):
"""Write the properties xml."""
worksheets_count = len(workbook.worksheets)
root = Element('{%s}Properties' % XPROPS_NS)
SubElement(root, '{%s}Application' % XPROPS_NS).text = 'Microsoft Excel'
SubElement(root, '{%s}DocSecurity' % XPROPS_NS).text = '0'
SubElement(root, '{%s}ScaleCrop' % XPROPS_NS).text = 'false'
SubElement(root, '{%s}Company' % XPROPS_NS)
SubElement(root, '{%s}LinksUpToDate' % XPROPS_NS).text = 'false'
SubElement(root, '{%s}SharedDoc' % XPROPS_NS).text = 'false'
SubElement(root, '{%s}HyperlinksChanged' % XPROPS_NS).text = 'false'
SubElement(root, '{%s}AppVersion' % XPROPS_NS).text = '12.0000'
# heading pairs part
heading_pairs = SubElement(root, '{%s}HeadingPairs' % XPROPS_NS)
vector = SubElement(heading_pairs, '{%s}vector' % VTYPES_NS,
{'size': '2', 'baseType': 'variant'})
variant = SubElement(vector, '{%s}variant' % VTYPES_NS)
SubElement(variant, '{%s}lpstr' % VTYPES_NS).text = 'Worksheets'
variant = SubElement(vector, '{%s}variant' % VTYPES_NS)
SubElement(variant, '{%s}i4' % VTYPES_NS).text = '%d' % worksheets_count
# title of parts
title_of_parts = SubElement(root, '{%s}TitlesOfParts' % XPROPS_NS)
vector = SubElement(title_of_parts, '{%s}vector' % VTYPES_NS,
{'size': '%d' % worksheets_count, 'baseType': 'lpstr'})
for ws in workbook.worksheets:
SubElement(vector, '{%s}lpstr' % VTYPES_NS).text = '%s' % ws.title
return tostring(root)
def write_root_rels(workbook):
"""Write the relationships xml."""
rels = RelationshipList()
rel = Relationship(type="officeDocument", target=ARC_WORKBOOK, id="rId1")
rels.append(rel)
rel = Relationship("", target=ARC_CORE, id='rId2',)
rel.type = "%s/metadata/core-properties" % PKG_REL_NS
rels.append(rel)
rel = Relationship("extended-properties", target=ARC_APP, id='rId3')
rels.append(rel)
if workbook.vba_archive is not None:
relation_tag = '{%s}Relationship' % PKG_REL_NS
# See if there was a customUI relation and reuse its id
arc = fromstring(workbook.vba_archive.read(ARC_ROOT_RELS))
rel_tags = arc.findall(relation_tag)
rId = None
for rel in rel_tags:
if rel.get('Target') == ARC_CUSTOM_UI:
rId = rel.get('Id')
break
if rId is not None:
vba = Relationship("", target=ARC_CUSTOM_UI, id=rId)
vba.type = CUSTOMUI_NS
rels.append(vba)
return tostring(rels.to_tree())
def write_workbook(workbook):
"""Write the core workbook xml."""
root = Element('{%s}workbook' % SHEET_MAIN_NS)
if LXML:
_nsmap = {'r':REL_NS}
root = Element('{%s}workbook' % SHEET_MAIN_NS, nsmap=_nsmap)
wb_props = {}
if workbook.code_name is not None:
wb_props['codeName'] = workbook.code_name
SubElement(root, '{%s}workbookPr' % SHEET_MAIN_NS, wb_props)
# book views
book_views = SubElement(root, '{%s}bookViews' % SHEET_MAIN_NS)
SubElement(book_views, '{%s}workbookView' % SHEET_MAIN_NS,
{'activeTab': '%d' % workbook._active_sheet_index}
)
# worksheets
sheets = SubElement(root, '{%s}sheets' % SHEET_MAIN_NS)
for i, sheet in enumerate(workbook._sheets, 1):
sheet_node = SubElement(
sheets, '{%s}sheet' % SHEET_MAIN_NS,
{'name': sheet.title, 'sheetId': '%d' % i,
'{%s}id' % REL_NS: 'rId%d' % i })
if not sheet.sheet_state == 'visible':
if len(workbook.worksheets) == 1:
raise ValueError("The only worksheet of a workbook cannot be hidden")
sheet_node.set('state', sheet.sheet_state)
# external references
if getattr(workbook, '_external_links', []):
external_references = SubElement(root, '{%s}externalReferences' % SHEET_MAIN_NS)
# need to match a counter with a workbook's relations
counter = len(workbook.worksheets) + 3 # strings, styles, theme
if workbook.vba_archive:
counter += 1
for idx, _ in enumerate(workbook._external_links, counter+1):
ext = Element("{%s}externalReference" % SHEET_MAIN_NS, {"{%s}id" % REL_NS:"rId%d" % idx})
external_references.append(ext)
# Defined names
defined_names = SubElement(root, '{%s}definedNames' % SHEET_MAIN_NS)
_write_defined_names(workbook, defined_names)
# Defined names -> autoFilter
for i, sheet in enumerate(workbook.worksheets):
auto_filter = sheet.auto_filter.ref
if not auto_filter:
continue
name = SubElement(
defined_names, '{%s}definedName' % SHEET_MAIN_NS,
dict(name='_xlnm._FilterDatabase', localSheetId=str(i), hidden='1'))
name.text = "'%s'!%s" % (sheet.title.replace("'", "''"),
absolute_coordinate(auto_filter))
SubElement(root, '{%s}calcPr' % SHEET_MAIN_NS,
{'calcId': '124519', 'fullCalcOnLoad': '1'})
return tostring(root)
def _write_defined_names(workbook, names):
"""
Append definedName elements to the definedNames node.
"""
for named_range in workbook.get_named_ranges():
attrs = dict(named_range)
if named_range.scope is not None:
attrs['localSheetId'] = safe_string(named_range.scope)
name = Element('{%s}definedName' % SHEET_MAIN_NS, attrs)
name.text = named_range.value
names.append(name)
def write_workbook_rels(workbook):
"""Write the workbook relationships xml."""
rels = RelationshipList()
rId = 0
for idx, _ in enumerate(workbook.worksheets, 1):
rId += 1
rel = Relationship(type='worksheet', target='worksheets/sheet%s.xml' % idx, id='rId%d' % rId)
rels.append(rel)
for idx, _ in enumerate(workbook.chartsheets, 1):
rId += 1
rel = Relationship(type='chartsheet', target='chartsheets/sheet%s.xml' % idx, id='rId%d' % rId)
rels.append(rel)
rId += 1
strings = Relationship(type='sharedStrings', target='sharedStrings.xml', id='rId%d' % rId)
rels.append(strings)
rId += 1
styles = Relationship(type='styles', target='styles.xml', id='rId%d' % rId)
rels.append(styles)
rId += 1
theme = Relationship(type='theme', target='theme/theme1.xml', id='rId%d' % rId)
rels.append(theme)
if workbook.vba_archive:
rId += 1
vba = Relationship(type='vbaProject', target='vbaProject.bin', id='rId%d' % rId)
vba.type ='http://schemas.microsoft.com/office/2006/relationships/vbaProject'
rels.append(vba)
external_links = workbook._external_links
if external_links:
for idx, link in enumerate(external_links, 1):
ext = Relationship(type='externalLink',
target='externalLinks/externalLink%d.xml' % idx,
id='rId%d' % (rId +idx))
rels.append(ext)
return tostring(rels.to_tree())
|