/usr/lib/python3/dist-packages/openpyxl/writer/excel.py is in python3-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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | from __future__ import absolute_import
# Copyright (c) 2010-2015 openpyxl
"""Write a .xlsx file."""
# Python stdlib imports
from io import BytesIO
from re import match
from zipfile import ZipFile, ZIP_DEFLATED
# package imports
from openpyxl.xml.constants import (
ARC_SHARED_STRINGS,
ARC_CONTENT_TYPES,
ARC_ROOT_RELS,
ARC_WORKBOOK_RELS,
ARC_APP, ARC_CORE,
ARC_THEME,
ARC_STYLE,
ARC_WORKBOOK,
PACKAGE_WORKSHEETS,
PACKAGE_CHARTSHEETS,
PACKAGE_DRAWINGS,
PACKAGE_CHARTS,
PACKAGE_IMAGES,
PACKAGE_XL
)
from openpyxl.drawing.spreadsheet_drawing import SpreadsheetDrawing
from openpyxl.xml.functions import tostring
from openpyxl.packaging.manifest import write_content_types
from openpyxl.writer.strings import write_string_table
from openpyxl.writer.workbook import (
write_root_rels,
write_workbook_rels,
write_properties_app,
write_workbook
)
from openpyxl.workbook.properties import write_properties
from openpyxl.writer.theme import write_theme
from openpyxl.writer.styles import StyleWriter
from .relations import write_rels
from openpyxl.writer.worksheet import write_worksheet
from openpyxl.workbook.names.external import (
write_external_link,
write_external_book_rel
)
from openpyxl.writer.comments import CommentWriter
ARC_VBA = ('xl/vba', r'xl/drawings/.*vmlDrawing\d\.vml', 'xl/ctrlProps', 'customUI',
'xl/activeX', r'xl/media/.*\.emf')
class ExcelWriter(object):
"""Write a workbook object to an Excel file."""
comment_writer = CommentWriter
def __init__(self, workbook):
self.workbook = workbook
self.workbook._drawings = []
self.style_writer = StyleWriter(workbook)
def write_data(self, archive, as_template=False):
"""Write the various xml files into the zip archive."""
# cleanup all worksheets
archive.writestr(ARC_ROOT_RELS, write_root_rels(self.workbook))
archive.writestr(ARC_WORKBOOK_RELS, write_workbook_rels(self.workbook))
archive.writestr(ARC_APP, write_properties_app(self.workbook))
archive.writestr(ARC_CORE, write_properties(self.workbook.properties))
if self.workbook.loaded_theme:
archive.writestr(ARC_THEME, self.workbook.loaded_theme)
else:
archive.writestr(ARC_THEME, write_theme())
archive.writestr(ARC_WORKBOOK, write_workbook(self.workbook))
if self.workbook.vba_archive:
vba_archive = self.workbook.vba_archive
for name in vba_archive.namelist():
for s in ARC_VBA:
if match(s, name):
archive.writestr(name, vba_archive.read(name))
break
self._write_charts(archive)
self._write_images(archive)
self._write_worksheets(archive)
self._write_chartsheets(archive)
self._write_string_table(archive)
self._write_external_links(archive)
archive.writestr(ARC_STYLE, self.style_writer.write_table())
manifest = write_content_types(self.workbook, as_template=as_template)
archive.writestr(ARC_CONTENT_TYPES, tostring(manifest.to_tree()))
def _write_string_table(self, archive):
archive.writestr(ARC_SHARED_STRINGS,
write_string_table(self.workbook.shared_strings))
def _write_images(self, archive):
for idx, ref in enumerate(self.workbook._images, 1):
img = ref()
if img is None:
continue
buf = BytesIO()
img.image.save(buf, format='PNG')
img._id = idx
archive.writestr(img._path, buf.getvalue())
def _write_charts(self, archive):
for idx, ref in enumerate(self.workbook._charts, 1):
chart = ref()
if not chart:
continue
chart._id = idx
archive.writestr(chart._path, tostring(chart._write()))
def _write_chartsheets(self, archive):
from openpyxl.packaging.relationship import Relationship, RelationshipList
from openpyxl.worksheet.drawing import Drawing
for idx, sheet in enumerate(self.workbook.chartsheets, 1):
if sheet._charts:
drawing = SpreadsheetDrawing()
drawing.charts = sheet._charts
self.workbook._drawings.append(drawing)
drawing_id = len(self.workbook._drawings)
drawingpath = "{0}/drawing{1}.xml".format(PACKAGE_DRAWINGS, drawing_id)
archive.writestr(drawingpath, tostring(drawing._write()))
archive.writestr(
"{0}/_rels/drawing{1}.xml.rels".format(
PACKAGE_DRAWINGS, drawing_id),
tostring(drawing._write_rels())
)
rel = Relationship(type="drawing", target="/" + drawingpath)
rels = RelationshipList()
rels.append(rel)
tree = rels.to_tree()
sheet.drawing.id = "rId{0}".format(len(rels))
archive.writestr(PACKAGE_CHARTSHEETS +
'/_rels/sheet%d.xml.rels' % idx, tostring(tree)
)
xml = tostring(sheet.to_tree())
archive.writestr(PACKAGE_CHARTSHEETS + '/sheet%d.xml' % idx, xml)
def _write_worksheets(self, archive):
comments_id = 0
vba_controls_id = 0
for i, sheet in enumerate(self.workbook.worksheets, 1):
xml = sheet._write(self.workbook.shared_strings)
archive.writestr(PACKAGE_WORKSHEETS + '/sheet%d.xml' % i , xml)
if sheet._charts or sheet._images:
drawing = SpreadsheetDrawing()
drawing.charts = sheet._charts
drawing.images = sheet._images
self.workbook._drawings.append(drawing)
drawing_id = len(self.workbook._drawings)
drawingpath = "{0}/drawing{1}.xml".format(PACKAGE_DRAWINGS, drawing_id)
archive.writestr(drawingpath, tostring(drawing._write()))
archive.writestr("{0}/_rels/drawing{1}.xml.rels".format(PACKAGE_DRAWINGS,
drawing_id), tostring(drawing._write_rels()))
for r in sheet._rels:
if "drawing" in r.type:
r.target = "/" + drawingpath
if sheet._comment_count > 0:
comments_id += 1
cw = self.comment_writer(sheet)
archive.writestr(PACKAGE_XL + '/comments%d.xml' % comments_id,
cw.write_comments())
archive.writestr(PACKAGE_XL + '/drawings/commentsDrawing%d.vml' % comments_id,
cw.write_comments_vml())
if sheet.vba_controls is not None:
vba_controls_id += 1
if (sheet._rels
or sheet._comment_count > 0
or sheet.vba_controls is not None):
rels = write_rels(sheet, comments_id=comments_id, vba_controls_id=vba_controls_id)
archive.writestr(PACKAGE_WORKSHEETS +
'/_rels/sheet%d.xml.rels' % i, tostring(rels))
def _write_external_links(self, archive):
"""Write links to external workbooks"""
wb = self.workbook
for idx, book in enumerate(wb._external_links, 1):
el = write_external_link(book.links)
rel = write_external_book_rel(book)
archive.writestr(
"{0}/externalLinks/externalLink{1}.xml".format(PACKAGE_XL, idx),
tostring(el)
)
archive.writestr(
"{0}/externalLinks/_rels/externalLink{1}.xml.rels".format(PACKAGE_XL, idx),
tostring(rel)
)
def save(self, filename, as_template=False):
"""Write data into the archive."""
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
self.write_data(archive, as_template=as_template)
archive.close()
def save_workbook(workbook, filename, as_template=False):
"""Save the given workbook on the filesystem under the name filename.
:param workbook: the workbook to save
:type workbook: :class:`openpyxl.workbook.Workbook`
:param filename: the path to which save the workbook
:type filename: string
:rtype: bool
"""
writer = ExcelWriter(workbook)
writer.save(filename, as_template=as_template)
return True
def save_virtual_workbook(workbook, as_template=False):
"""Return an in-memory workbook, suitable for a Django response."""
writer = ExcelWriter(workbook)
temp_buffer = BytesIO()
try:
archive = ZipFile(temp_buffer, 'w', ZIP_DEFLATED, allowZip64=True)
writer.write_data(archive, as_template=as_template)
finally:
archive.close()
virtual_workbook = temp_buffer.getvalue()
temp_buffer.close()
return virtual_workbook
|