/usr/share/pyshared/openpyxl/writer/drawings.py is in python-openpyxl 1.7.0+ds1-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 | # coding=UTF-8
# Copyright (c) 2010-2011 openpyxl
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# @license: http://www.opensource.org/licenses/mit-license.php
# @author: see AUTHORS file
from openpyxl.shared.xmltools import Element, SubElement, get_document_content
class DrawingWriter(object):
""" one main drawing file per sheet """
def __init__(self, sheet):
self._sheet = sheet
def write(self):
""" write drawings for one sheet in one file """
root = Element('xdr:wsDr',
{'xmlns:xdr' : "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing",
'xmlns:a' : "http://schemas.openxmlformats.org/drawingml/2006/main"})
for i, chart in enumerate(self._sheet._charts):
drawing = chart.drawing
# anchor = SubElement(root, 'xdr:twoCellAnchor')
# (start_row, start_col), (end_row, end_col) = drawing.coordinates
# # anchor coordinates
# _from = SubElement(anchor, 'xdr:from')
# x = SubElement(_from, 'xdr:col').text = str(start_col)
# x = SubElement(_from, 'xdr:colOff').text = '0'
# x = SubElement(_from, 'xdr:row').text = str(start_row)
# x = SubElement(_from, 'xdr:rowOff').text = '0'
# _to = SubElement(anchor, 'xdr:to')
# x = SubElement(_to, 'xdr:col').text = str(end_col)
# x = SubElement(_to, 'xdr:colOff').text = '0'
# x = SubElement(_to, 'xdr:row').text = str(end_row)
# x = SubElement(_to, 'xdr:rowOff').text = '0'
# we only support absolute anchor atm (TODO: oneCellAnchor, twoCellAnchor
x, y, w, h = drawing.get_emu_dimensions()
anchor = SubElement(root, 'xdr:absoluteAnchor')
SubElement(anchor, 'xdr:pos', {'x':str(x), 'y':str(y)})
SubElement(anchor, 'xdr:ext', {'cx':str(w), 'cy':str(h)})
# graph frame
frame = SubElement(anchor, 'xdr:graphicFrame', {'macro':''})
name = SubElement(frame, 'xdr:nvGraphicFramePr')
SubElement(name, 'xdr:cNvPr', {'id':'%s' % i, 'name':'Graphique %s' % i})
SubElement(name, 'xdr:cNvGraphicFramePr')
frm = SubElement(frame, 'xdr:xfrm')
# no transformation
SubElement(frm, 'a:off', {'x':'0', 'y':'0'})
SubElement(frm, 'a:ext', {'cx':'0', 'cy':'0'})
graph = SubElement(frame, 'a:graphic')
data = SubElement(graph, 'a:graphicData',
{'uri':'http://schemas.openxmlformats.org/drawingml/2006/chart'})
SubElement(data, 'c:chart',
{ 'xmlns:c':'http://schemas.openxmlformats.org/drawingml/2006/chart',
'xmlns:r':'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
'r:id':'rId%s' % (i + 1)})
SubElement(anchor, 'xdr:clientData')
for i, img in enumerate(self._sheet._images):
drawing = img.drawing
x, y, w, h = drawing.get_emu_dimensions()
anchor = SubElement(root, 'xdr:absoluteAnchor')
SubElement(anchor, 'xdr:pos', {'x':str(x), 'y':str(y)})
SubElement(anchor, 'xdr:ext', {'cx':str(w), 'cy':str(h)})
pic = SubElement(anchor, 'xdr:pic')
name = SubElement(pic, 'xdr:nvPicPr')
SubElement(name, 'xdr:cNvPr', {'id':'%s' % i, 'name':'Picture %s' % i})
SubElement(SubElement(name, 'xdr:cNvPicPr'), 'a:picLocks', {'noChangeAspect':"1" if img.nochangeaspect else '0','noChangeArrowheads':"1" if img.nochangearrowheads else '0'})
blipfill = SubElement(pic, 'xdr:blipFill')
SubElement(blipfill, 'a:blip', {
'xmlns:r': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
'r:embed': 'rId%s' % (i + 1),
'cstate':'print'
})
SubElement(blipfill, 'a:srcRect')
SubElement(SubElement(blipfill, 'a:stretch'), 'a:fillRect')
sppr = SubElement(pic, 'xdr:spPr', {'bwMode':'auto'})
frm = SubElement(sppr, 'a:xfrm')
# no transformation
SubElement(frm, 'a:off', {'x':'0', 'y':'0'})
SubElement(frm, 'a:ext', {'cx':'0', 'cy':'0'})
SubElement(SubElement(sppr, 'a:prstGeom', {'prst':'rect'}), 'a:avLst')
SubElement(sppr, 'a:noFill')
ln = SubElement(sppr, 'a:ln', {'w':'1'})
SubElement(ln, 'a:noFill')
SubElement(ln, 'a:miter', {'lim':'800000'})
SubElement(ln, 'a:headEnd')
SubElement(ln, 'a:tailEnd', {'type':'none', 'w':'med', 'len':'med'})
SubElement(sppr, 'a:effectLst')
SubElement(anchor, 'xdr:clientData')
return get_document_content(root)
def write_rels(self, chart_id, image_id):
root = Element('Relationships',
{'xmlns' : 'http://schemas.openxmlformats.org/package/2006/relationships'})
i = 0
for i, chart in enumerate(self._sheet._charts):
attrs = {'Id' : 'rId%s' % (i + 1),
'Type' : 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
'Target' : '../charts/chart%s.xml' % (chart_id + i) }
SubElement(root, 'Relationship', attrs)
for j, img in enumerate(self._sheet._images):
attrs = {'Id' : 'rId%s' % (i + j + 1),
'Type' : 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
'Target' : '../media/image%s.png' % (image_id + j) }
SubElement(root, 'Relationship', attrs)
return get_document_content(root)
class ShapeWriter(object):
""" one file per shape """
schema = "http://schemas.openxmlformats.org/drawingml/2006/main"
def __init__(self, shapes):
self._shapes = shapes
def write(self, shape_id):
root = Element('c:userShapes', {'xmlns:c' : 'http://schemas.openxmlformats.org/drawingml/2006/chart'})
for shape in self._shapes:
anchor = SubElement(root, 'cdr:relSizeAnchor',
{'xmlns:cdr' : "http://schemas.openxmlformats.org/drawingml/2006/chartDrawing"})
xstart, ystart, xend, yend = shape.get_coordinates()
_from = SubElement(anchor, 'cdr:from')
SubElement(_from, 'cdr:x').text = str(xstart)
SubElement(_from, 'cdr:y').text = str(ystart)
_to = SubElement(anchor, 'cdr:to')
SubElement(_to, 'cdr:x').text = str(xend)
SubElement(_to, 'cdr:y').text = str(yend)
sp = SubElement(anchor, 'cdr:sp', {'macro':'', 'textlink':''})
nvspr = SubElement(sp, 'cdr:nvSpPr')
SubElement(nvspr, 'cdr:cNvPr', {'id':str(shape_id), 'name':'shape %s' % shape_id})
SubElement(nvspr, 'cdr:cNvSpPr')
sppr = SubElement(sp, 'cdr:spPr')
frm = SubElement(sppr, 'a:xfrm', {'xmlns:a':self.schema})
# no transformation
SubElement(frm, 'a:off', {'x':'0', 'y':'0'})
SubElement(frm, 'a:ext', {'cx':'0', 'cy':'0'})
prstgeom = SubElement(sppr, 'a:prstGeom', {'xmlns:a':self.schema, 'prst':str(shape.style)})
SubElement(prstgeom, 'a:avLst')
fill = SubElement(sppr, 'a:solidFill', {'xmlns:a':self.schema})
SubElement(fill, 'a:srgbClr', {'val':shape.color})
border = SubElement(sppr, 'a:ln', {'xmlns:a':self.schema, 'w':str(shape._border_width)})
sf = SubElement(border, 'a:solidFill')
SubElement(sf, 'a:srgbClr', {'val':shape.border_color})
self._write_style(sp)
self._write_text(sp, shape)
shape_id += 1
return get_document_content(root)
def _write_text(self, node, shape):
""" write text in the shape """
tx_body = SubElement(node, 'cdr:txBody')
SubElement(tx_body, 'a:bodyPr', {'xmlns:a':self.schema, 'vertOverflow':'clip'})
SubElement(tx_body, 'a:lstStyle',
{'xmlns:a':self.schema})
p = SubElement(tx_body, 'a:p', {'xmlns:a':self.schema})
if shape.text:
r = SubElement(p, 'a:r')
rpr = SubElement(r, 'a:rPr', {'lang':'en-US'})
fill = SubElement(rpr, 'a:solidFill')
SubElement(fill, 'a:srgbClr', {'val':shape.text_color})
SubElement(r, 'a:t').text = shape.text
else:
SubElement(p, 'a:endParaRPr', {'lang':'en-US'})
def _write_style(self, node):
""" write style theme """
style = SubElement(node, 'cdr:style')
ln_ref = SubElement(style, 'a:lnRef', {'xmlns:a':self.schema, 'idx':'2'})
scheme_clr = SubElement(ln_ref, 'a:schemeClr', {'val':'accent1'})
SubElement(scheme_clr, 'a:shade', {'val':'50000'})
fill_ref = SubElement(style, 'a:fillRef', {'xmlns:a':self.schema, 'idx':'1'})
SubElement(fill_ref, 'a:schemeClr', {'val':'accent1'})
effect_ref = SubElement(style, 'a:effectRef', {'xmlns:a':self.schema, 'idx':'0'})
SubElement(effect_ref, 'a:schemeClr', {'val':'accent1'})
font_ref = SubElement(style, 'a:fontRef', {'xmlns:a':self.schema, 'idx':'minor'})
SubElement(font_ref, 'a:schemeClr', {'val':'lt1'})
|