/usr/lib/python2.7/dist-packages/openpyxl/writer/write_only.py is in python-openpyxl 2.4.9-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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | from __future__ import absolute_import
# Copyright (c) 2010-2017 openpyxl
"""Write worksheets to xml representations in an optimized way"""
import atexit
from inspect import isgenerator
import os
from tempfile import NamedTemporaryFile
from zipfile import ZipFile, ZIP_DEFLATED
from openpyxl.cell import Cell, WriteOnlyCell
from openpyxl.drawing.spreadsheet_drawing import SpreadsheetDrawing
from openpyxl.worksheet import Worksheet
from openpyxl.workbook.child import _WorkbookChild
from openpyxl.worksheet.related import Related
from openpyxl.worksheet.dimensions import SheetFormatProperties
from openpyxl.utils.exceptions import WorkbookAlreadySaved
from .etree_worksheet import write_cell
from .excel import ExcelWriter
from .worksheet import write_drawing, write_conditional_formatting
from openpyxl.xml.constants import SHEET_MAIN_NS
from openpyxl.xml.functions import xmlfile, Element
ALL_TEMP_FILES = []
@atexit.register
def _openpyxl_shutdown():
global ALL_TEMP_FILES
for path in ALL_TEMP_FILES:
if os.path.exists(path):
os.remove(path)
def create_temporary_file(suffix=''):
fobj = NamedTemporaryFile(mode='w+', suffix=suffix,
prefix='openpyxl.', delete=False)
filename = fobj.name
ALL_TEMP_FILES.append(filename)
return filename
class WriteOnlyWorksheet(_WorkbookChild):
"""
Streaming worksheet. Optimised to reduce memory by writing rows just in
time.
Cells can be styled and have comments Styles for rows and columns
must be applied before writing cells
"""
__saved = False
writer = None
_rel_type = Worksheet._rel_type
_path = Worksheet._path
mime_type = Worksheet.mime_type
def __init__(self, parent, title):
super(WriteOnlyWorksheet, self).__init__(parent, title)
self._max_col = 0
self._max_row = 0
self._fileobj_name = create_temporary_file()
# Methods from Worksheet
self._add_row = Worksheet._add_row.__get__(self)
self._add_column = Worksheet._add_column.__get__(self)
self.add_chart = Worksheet.add_chart.__get__(self)
self.add_image = Worksheet.add_image.__get__(self)
self.add_table = Worksheet.add_table.__get__(self)
setup = Worksheet._setup.__get__(self)
setup()
self.print_titles = Worksheet.print_titles.__get__(self)
self.sheet_view = Worksheet.sheet_view.__get__(self)
@property
def freeze_panes(self):
return Worksheet.freeze_panes.__get__(self)
@freeze_panes.setter
def freeze_panes(self, value):
Worksheet.freeze_panes.__set__(self, value)
@property
def print_title_cols(self):
return Worksheet.print_title_cols.__get__(self)
@print_title_cols.setter
def print_title_cols(self, value):
Worksheet.print_title_cols.__set__(self, value)
@property
def print_title_rows(self):
return Worksheet.print_title_rows.__get__(self)
@print_title_rows.setter
def print_title_rows(self, value):
Worksheet.print_title_rows.__set__(self, value)
@property
def print_area(self):
return Worksheet.print_area.__get__(self)
@print_area.setter
def print_area(self, value):
Worksheet.print_area.__set__(self, value)
@property
def filename(self):
return self._fileobj_name
def _write_header(self):
"""
Generator that creates the XML file and the sheet header
"""
with xmlfile(self.filename) as xf:
with xf.element("worksheet", xmlns=SHEET_MAIN_NS):
if self.sheet_properties:
pr = self.sheet_properties.to_tree()
xf.write(pr)
xf.write(self.views.to_tree())
cols = self.column_dimensions.to_tree()
self.sheet_format.outlineLevelCol = self.column_dimensions.max_outline
xf.write(self.sheet_format.to_tree())
if cols is not None:
xf.write(cols)
with xf.element("sheetData"):
cell = WriteOnlyCell(self)
try:
while True:
row = (yield)
row_idx = self._max_row
attrs = {'r': '%d' % row_idx}
if row_idx in self.row_dimensions:
dim = self.row_dimensions[row_idx]
attrs.update(dict(dim))
with xf.element("row", attrs):
for col_idx, value in enumerate(row, 1):
if value is None:
continue
try:
cell.value = value
except ValueError:
if isinstance(value, Cell):
cell = value
else:
raise ValueError
cell.col_idx = col_idx
cell.row = row_idx
styled = cell.has_style
write_cell(xf, self, cell, styled)
if styled: # styled cell or datetime
cell = WriteOnlyCell(self)
except GeneratorExit:
pass
if self.protection.sheet:
xf.write(self.protection.to_tree())
if self.auto_filter.ref:
xf.write(self.auto_filter.to_tree())
if self.sort_state.ref:
xf.write(self.sort_state.to_tree())
if self.conditional_formatting:
cfs = write_conditional_formatting(self)
for cf in cfs:
xf.write(cf)
if self.data_validations.count:
xf.write(self.data_validations.to_tree())
drawing = write_drawing(self)
if drawing is not None:
xf.write(drawing)
if self._comments:
legacyDrawing = Related(id="anysvml")
xml = legacyDrawing.to_tree("legacyDrawing")
xf.write(xml)
def close(self):
if self.__saved:
self._already_saved()
if self.writer is None:
self.writer = self._write_header()
next(self.writer)
self.writer.close()
self.__saved = True
def _cleanup(self):
os.remove(self.filename)
def append(self, row):
"""
:param row: iterable containing values to append
:type row: iterable
"""
if (not isgenerator(row) and
not isinstance(row, (list, tuple, range))
):
self._invalid_row(row)
self._max_row += 1
if self.writer is None:
self.writer = self._write_header()
next(self.writer)
try:
self.writer.send(row)
except StopIteration:
self._already_saved()
def _already_saved(self):
raise WorkbookAlreadySaved('Workbook has already been saved and cannot be modified or saved anymore.')
def _invalid_row(self, iterable):
raise TypeError('Value must be a list, tuple, range or a generator Supplied value is {0}'.format(
type(iterable))
)
def _write(self):
self._drawing = SpreadsheetDrawing()
self._drawing.charts = self._charts
self._drawing.images = self._images
self.close()
with open(self.filename) as src:
out = src.read()
self._cleanup()
return out
def save_dump(workbook, filename):
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
if workbook.worksheets == []:
workbook.create_sheet()
writer = ExcelWriter(workbook, archive)
writer.save(filename)
return True
|