/usr/lib/python3/dist-packages/xlsxwriter/drawing.py is in python3-xlsxwriter 0.5.2-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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 | ###############################################################################
#
# Drawing - A class for writing the Excel XLSX Drawing file.
#
# Copyright 2013, John McNamara, jmcnamara@cpan.org
#
from . import xmlwriter
class Drawing(xmlwriter.XMLwriter):
"""
A class for writing the Excel XLSX Drawing file.
"""
###########################################################################
#
# Public API.
#
###########################################################################
def __init__(self):
"""
Constructor.
"""
super(Drawing, self).__init__()
self.drawings = []
self.embedded = 0
self.orientation = 0
###########################################################################
#
# Private API.
#
###########################################################################
def _assemble_xml_file(self):
# Assemble and write the XML file.
# Write the XML declaration.
self._xml_declaration()
# Write the xdr:wsDr element.
self._write_drawing_workspace()
if self.embedded:
index = 1
for dimensions in self.drawings:
# Write the xdr:twoCellAnchor element.
self._write_two_cell_anchor(index, dimensions)
index += 1
else:
# Write the xdr:absoluteAnchor element.
self._write_absolute_anchor(1)
self._xml_end_tag('xdr:wsDr')
# Close the file.
self._xml_close()
def _add_drawing_object(self, drawing_object):
# Add a chart, image or shape sub object to the drawing.
self.drawings.append(drawing_object)
###########################################################################
#
# XML methods.
#
###########################################################################
def _write_drawing_workspace(self):
# Write the <xdr:wsDr> element.
schema = 'http://schemas.openxmlformats.org/drawingml/'
xmlns_xdr = schema + '2006/spreadsheetDrawing'
xmlns_a = schema + '2006/main'
attributes = [
('xmlns:xdr', xmlns_xdr),
('xmlns:a', xmlns_a),
]
self._xml_start_tag('xdr:wsDr', attributes)
def _write_two_cell_anchor(self, index, dimensions):
# Write the <xdr:twoCellAnchor> element.
anchor_type = dimensions[0]
col_from = dimensions[1]
row_from = dimensions[2]
col_from_offset = dimensions[3]
row_from_offset = dimensions[4]
col_to = dimensions[5]
row_to = dimensions[6]
col_to_offset = dimensions[7]
row_to_offset = dimensions[8]
col_absolute = dimensions[9]
row_absolute = dimensions[10]
width = dimensions[11]
height = dimensions[12]
description = dimensions[13]
shape = dimensions[14]
attributes = []
# Add attribute for images.
if anchor_type == 2:
attributes.append(('editAs', 'oneCell'))
# Add editAs attribute for shapes.
if shape and shape.editAs:
attributes.append(('editAs', shape.editAs))
self._xml_start_tag('xdr:twoCellAnchor', attributes)
# Write the xdr:from element.
self._write_from(
col_from,
row_from,
col_from_offset,
row_from_offset)
# Write the xdr:from element.
self._write_to(
col_to,
row_to,
col_to_offset,
row_to_offset)
if anchor_type == 1:
# Graphic frame.
# Write the xdr:graphicFrame element for charts.
self._write_graphic_frame(index, description)
elif anchor_type == 2:
# Write the xdr:pic element.
self._write_pic(index, col_absolute, row_absolute, width,
height, description)
else:
# Write the xdr:sp element for shapes.
self._write_sp(index, col_absolute, row_absolute, width, height,
shape)
# Write the xdr:clientData element.
self._write_client_data()
self._xml_end_tag('xdr:twoCellAnchor')
def _write_absolute_anchor(self, frame_index):
self._xml_start_tag('xdr:absoluteAnchor')
# Write the <xdr:absoluteAnchor> element.
# Different co-ordinates for horizontal (= 0) and vertical (= 1).
if self.orientation == 0:
# Write the xdr:pos element.
self._write_pos(0, 0)
# Write the xdr:ext element.
self._write_ext(9308969, 6078325)
else:
# Write the xdr:pos element.
self._write_pos(0, -47625)
# Write the xdr:ext element.
self._write_ext(6162675, 6124575)
# Write the xdr:graphicFrame element.
self._write_graphic_frame(frame_index)
# Write the xdr:clientData element.
self._write_client_data()
self._xml_end_tag('xdr:absoluteAnchor')
def _write_from(self, col, row, col_offset, row_offset):
# Write the <xdr:from> element.
self._xml_start_tag('xdr:from')
# Write the xdr:col element.
self._write_col(col)
# Write the xdr:colOff element.
self._write_col_off(col_offset)
# Write the xdr:row element.
self._write_row(row)
# Write the xdr:rowOff element.
self._write_row_off(row_offset)
self._xml_end_tag('xdr:from')
def _write_to(self, col, row, col_offset, row_offset):
# Write the <xdr:to> element.
self._xml_start_tag('xdr:to')
# Write the xdr:col element.
self._write_col(col)
# Write the xdr:colOff element.
self._write_col_off(col_offset)
# Write the xdr:row element.
self._write_row(row)
# Write the xdr:rowOff element.
self._write_row_off(row_offset)
self._xml_end_tag('xdr:to')
def _write_col(self, data):
# Write the <xdr:col> element.
self._xml_data_element('xdr:col', data)
def _write_col_off(self, data):
# Write the <xdr:colOff> element.
self._xml_data_element('xdr:colOff', data)
def _write_row(self, data):
# Write the <xdr:row> element.
self._xml_data_element('xdr:row', data)
def _write_row_off(self, data):
# Write the <xdr:rowOff> element.
self._xml_data_element('xdr:rowOff', data)
def _write_pos(self, x, y):
# Write the <xdr:pos> element.
attributes = [('x', x), ('y', y)]
self._xml_empty_tag('xdr:pos', attributes)
def _write_ext(self, cx, cy):
# Write the <xdr:ext> element.
attributes = [('cx', cx), ('cy', cy)]
self._xml_empty_tag('xdr:ext', attributes)
def _write_graphic_frame(self, index, name=None):
# Write the <xdr:graphicFrame> element.
attributes = [('macro', '')]
self._xml_start_tag('xdr:graphicFrame', attributes)
# Write the xdr:nvGraphicFramePr element.
self._write_nv_graphic_frame_pr(index, name)
# Write the xdr:xfrm element.
self._write_xfrm()
# Write the a:graphic element.
self._write_atag_graphic(index)
self._xml_end_tag('xdr:graphicFrame')
def _write_nv_graphic_frame_pr(self, index, name):
# Write the <xdr:nvGraphicFramePr> element.
if not name:
name = 'Chart ' + str(index)
self._xml_start_tag('xdr:nvGraphicFramePr')
# Write the xdr:cNvPr element.
self._write_c_nv_pr(index + 1, name)
# Write the xdr:cNvGraphicFramePr element.
self._write_c_nv_graphic_frame_pr()
self._xml_end_tag('xdr:nvGraphicFramePr')
def _write_c_nv_pr(self, index, name, descr=None):
# Write the <xdr:cNvPr> element.
attributes = [('id', index), ('name', name)]
# Add description attribute for images.
if descr is not None:
attributes.append(('descr', descr))
self._xml_empty_tag('xdr:cNvPr', attributes)
def _write_c_nv_graphic_frame_pr(self):
# Write the <xdr:cNvGraphicFramePr> element.
if self.embedded:
self._xml_empty_tag('xdr:cNvGraphicFramePr')
else:
self._xml_start_tag('xdr:cNvGraphicFramePr')
# Write the a:graphicFrameLocks element.
self._write_a_graphic_frame_locks()
self._xml_end_tag('xdr:cNvGraphicFramePr')
def _write_a_graphic_frame_locks(self):
# Write the <a:graphicFrameLocks> element.
attributes = [('noGrp', 1)]
self._xml_empty_tag('a:graphicFrameLocks', attributes)
def _write_xfrm(self):
# Write the <xdr:xfrm> element.
self._xml_start_tag('xdr:xfrm')
# Write the xfrmOffset element.
self._write_xfrm_offset()
# Write the xfrmOffset element.
self._write_xfrm_extension()
self._xml_end_tag('xdr:xfrm')
def _write_xfrm_offset(self):
# Write the <a:off> xfrm sub-element.
attributes = [
('x', 0),
('y', 0),
]
self._xml_empty_tag('a:off', attributes)
def _write_xfrm_extension(self):
# Write the <a:ext> xfrm sub-element.
attributes = [
('cx', 0),
('cy', 0),
]
self._xml_empty_tag('a:ext', attributes)
def _write_atag_graphic(self, index):
# Write the <a:graphic> element.
self._xml_start_tag('a:graphic')
# Write the a:graphicData element.
self._write_atag_graphic_data(index)
self._xml_end_tag('a:graphic')
def _write_atag_graphic_data(self, index):
# Write the <a:graphicData> element.
uri = 'http://schemas.openxmlformats.org/drawingml/2006/chart'
attributes = [('uri', uri,)]
self._xml_start_tag('a:graphicData', attributes)
# Write the c:chart element.
self._write_c_chart('rId' + str(index))
self._xml_end_tag('a:graphicData')
def _write_c_chart(self, r_id):
# Write the <c:chart> element.
schema = 'http://schemas.openxmlformats.org/'
xmlns_c = schema + 'drawingml/2006/chart'
xmlns_r = schema + 'officeDocument/2006/relationships'
attributes = [
('xmlns:c', xmlns_c),
('xmlns:r', xmlns_r),
('r:id', r_id),
]
self._xml_empty_tag('c:chart', attributes)
def _write_client_data(self):
# Write the <xdr:clientData> element.
self._xml_empty_tag('xdr:clientData')
def _write_sp(self, index, col_absolute, row_absolute,
width, height, shape):
# Write the <xdr:sp> element.
if shape and shape.connect:
attributes = [('macro', '')]
self._xml_start_tag('xdr:cxnSp', attributes)
# Write the xdr:nvCxnSpPr element.
self._write_nv_cxn_sp_pr(index, shape)
# Write the xdr:spPr element.
self._write_xdr_sp_pr(index, col_absolute, row_absolute, width,
height, shape)
self._xml_end_tag('xdr:cxnSp')
else:
# Add attribute for shapes.
attributes = [('macro', ''), ('textlink', '')]
self._xml_start_tag('xdr:sp', attributes)
# Write the xdr:nvSpPr element.
self._write_nv_sp_pr(index, shape)
# Write the xdr:spPr element.
self._write_xdr_sp_pr(index, col_absolute, row_absolute, width,
height, shape)
# Write the xdr:txBody element.
if shape.text:
self._write_tx_body(col_absolute, row_absolute, width, height,
shape)
self._xml_end_tag('xdr:sp')
def _write_nv_cxn_sp_pr(self, index, shape):
# Write the <xdr:nvCxnSpPr> element.
self._xml_start_tag('xdr:nvCxnSpPr')
shape.name = shape.type + ' ' + index
if shape.name is not None:
self._write_c_nv_pr(shape.id, shape.name)
self._xml_start_tag('xdr:cNvCxnSpPr')
attributes = [('noChangeShapeType', '1')]
self._xml_empty_tag('a:cxnSpLocks', attributes)
if shape.start:
attributes = [('id', shape.start), ('idx', shape.start_index)]
self._xml_empty_tag('a:stCxn', attributes)
if shape.end:
attributes = [('id', shape.end), ('idx', shape.end_index)]
self._xml_empty_tag('a:endCxn', attributes)
self._xml_end_tag('xdr:cNvCxnSpPr')
self._xml_end_tag('xdr:nvCxnSpPr')
def _write_nv_sp_pr(self, index, shape):
# Write the <xdr:NvSpPr> element.
attributes = []
self._xml_start_tag('xdr:nvSpPr')
shape_name = shape.type + ' ' + index
self._write_c_nv_pr(shape.id, shape_name)
if shape.txBox:
attributes = [('txBox', 1)]
self._xml_start_tag('xdr:cNvSpPr', attributes)
attributes = [('noChangeArrowheads', '1')]
self._xml_empty_tag('a:spLocks', attributes)
self._xml_end_tag('xdr:cNvSpPr')
self._xml_end_tag('xdr:nvSpPr')
def _write_pic(self, index, col_absolute, row_absolute,
width, height, description):
# Write the <xdr:pic> element.
self._xml_start_tag('xdr:pic')
# Write the xdr:nvPicPr element.
self._write_nv_pic_pr(index, description)
# Write the xdr:blipFill element.
self._write_blip_fill(index)
# Pictures are rectangle shapes by default.
shape = {'type': 'rect'}
# Write the xdr:spPr element.
self._write_sp_pr(col_absolute, row_absolute, width, height,
shape)
self._xml_end_tag('xdr:pic')
def _write_nv_pic_pr(self, index, description):
# Write the <xdr:nvPicPr> element.
self._xml_start_tag('xdr:nvPicPr')
# Write the xdr:cNvPr element.
self._write_c_nv_pr(index + 1, 'Picture ' + str(index), description)
# Write the xdr:cNvPicPr element.
self._write_c_nv_pic_pr()
self._xml_end_tag('xdr:nvPicPr')
def _write_c_nv_pic_pr(self):
# Write the <xdr:cNvPicPr> element.
self._xml_start_tag('xdr:cNvPicPr')
# Write the a:picLocks element.
self._write_a_pic_locks()
self._xml_end_tag('xdr:cNvPicPr')
def _write_a_pic_locks(self):
# Write the <a:picLocks> element.
attributes = [('noChangeAspect', 1)]
self._xml_empty_tag('a:picLocks', attributes)
def _write_blip_fill(self, index):
# Write the <xdr:blipFill> element.
self._xml_start_tag('xdr:blipFill')
# Write the a:blip element.
self._write_a_blip(index)
# Write the a:stretch element.
self._write_a_stretch()
self._xml_end_tag('xdr:blipFill')
def _write_a_blip(self, index):
# Write the <a:blip> element.
schema = 'http://schemas.openxmlformats.org/officeDocument/'
xmlns_r = schema + '2006/relationships'
r_embed = 'rId' + str(index)
attributes = [
('xmlns:r', xmlns_r),
('r:embed', r_embed)]
self._xml_empty_tag('a:blip', attributes)
def _write_a_stretch(self):
# Write the <a:stretch> element.
self._xml_start_tag('a:stretch')
# Write the a:fillRect element.
self._write_a_fill_rect()
self._xml_end_tag('a:stretch')
def _write_a_fill_rect(self):
# Write the <a:fillRect> element.
self._xml_empty_tag('a:fillRect')
def _write_sp_pr(self, col_absolute, row_absolute, width, height,
shape={}):
# Write the <xdr:spPr> element, for charts.
self._xml_start_tag('xdr:spPr')
# Write the a:xfrm element.
self._write_a_xfrm(col_absolute, row_absolute, width, height)
# Write the a:prstGeom element.
self._write_a_prst_geom(shape)
self._xml_end_tag('xdr:spPr')
def _write_xdr_sp_pr(self, index, col_absolute, row_absolute, width,
height, shape={}):
# Write the <xdr:spPr> element for shapes.
attributes = [('bwMode', 'auto')]
self._xml_start_tag('xdr:spPr', attributes)
# Write the a:xfrm element.
self._write_a_xfrm(col_absolute, row_absolute, width, height, shape)
# Write the a:prstGeom element.
self._write_a_prst_geom(shape)
fill = shape.fill
if len(fill) > 1:
# Write the a:solidFill element.
self._write_a_solid_fill(fill)
else:
self._xml_empty_tag('a:noFill')
# Write the a:ln element.
self._write_a_ln(shape)
self._xml_end_tag('xdr:spPr')
def _write_a_xfrm(self, col_absolute, row_absolute, width, height,
shape={}):
# Write the <a:xfrm> element.
attributes = []
if "rotation" in shape:
rotation = shape.rotation
rotation *= 60000
attributes.append(('rot', rotation))
if 'flip_h' in shape:
attributes.append(('flipH', 1))
if 'flip_v' in shape:
attributes.append(('flipV', 1))
self._xml_start_tag('a:xfrm', attributes)
# Write the a:off element.
self._write_a_off(col_absolute, row_absolute)
# Write the a:ext element.
self._write_a_ext(width, height)
self._xml_end_tag('a:xfrm')
def _write_a_off(self, x, y):
# Write the <a:off> element.
attributes = [
('x', x),
('y', y),
]
self._xml_empty_tag('a:off', attributes)
def _write_a_ext(self, cx, cy):
# Write the <a:ext> element.
attributes = [
('cx', cx),
('cy', cy),
]
self._xml_empty_tag('a:ext', attributes)
def _write_a_prst_geom(self, shape={}):
# Write the <a:prstGeom> element.
attributes = []
if 'type' in shape:
attributes = [('prst', shape['type'])]
self._xml_start_tag('a:prstGeom', attributes)
# Write the a:avLst element.
self._write_a_av_lst(shape)
self._xml_end_tag('a:prstGeom')
def _write_a_av_lst(self, shape={}):
# Write the <a:avLst> element.
adjustments = []
if 'adjustments' in shape:
adjustments = shape['adjustments']
if adjustments:
self._xml_start_tag('a:avLst')
i = 0
for adj in adjustments:
i += 1
# Only connectors have multiple adjustments.
if 'connect' in shape:
suffix = i
else:
suffix = ''
# Scale Adjustments: 100,000 = 100%.
adj_int = str(int(adj * 1000))
attributes = [('name', 'adj' + suffix),
('fmla', 'val' + adj_int)]
self._xml_empty_tag('a:gd', attributes)
self._xml_end_tag('a:avLst')
else:
self._xml_empty_tag('a:avLst')
def _write_a_solid_fill(self, rgb):
# Write the <a:solidFill> element.
if not rgb is not None:
rgb = '000000'
attributes = [('val', rgb)]
self._xml_start_tag('a:solidFill')
self._xml_empty_tag('a:srgbClr', attributes)
self._xml_end_tag('a:solidFill')
def _write_a_ln(self, shape={}):
# Write the <a:ln> element.
weight = shape.line_weight
attributes = [('w', weight * 9525)]
self._xml_start_tag('a:ln', attributes)
line = shape.line
if len(line) > 1:
# Write the a:solidFill element.
self._write_a_solid_fill(line)
else:
self._xml_empty_tag('a:noFill')
if shape.line_type:
attributes = [('val', shape.line_type)]
self._xml_empty_tag('a:prstDash', attributes)
if shape.connect:
self._xml_empty_tag('a:round')
else:
attributes = [('lim', 800000)]
self._xml_empty_tag('a:miter', attributes)
self._xml_empty_tag('a:headEnd')
self._xml_empty_tag('a:tailEnd')
self._xml_end_tag('a:ln')
def _write_tx_body(self, col_absolute, row_absolute, width, height, shape):
# Write the <xdr:txBody> element.
attributes = [
('vertOverflow', "clip"),
('wrap', "square"),
('lIns', "27432"),
('tIns', "22860"),
('rIns', "27432"),
('bIns', "22860"),
('anchor', shape.valign),
('upright', "1"),
]
self._xml_start_tag('xdr:txBody')
self._xml_empty_tag('a:bodyPr', attributes)
self._xml_empty_tag('a:lstStyle')
self._xml_start_tag('a:p')
rotation = shape.format.rotation
if not rotation is not None:
rotation = 0
rotation *= 60000
attributes = [('algn', shape.align), ('rtl', rotation)]
self._xml_start_tag('a:pPr', attributes)
attributes = [('sz', "1000")]
self._xml_empty_tag('a:defRPr', attributes)
self._xml_end_tag('a:pPr')
self._xml_start_tag('a:r')
size = shape.format.size
if not size is not None:
size = 8
size *= 100
bold = shape.format.bold
if not bold is not None:
bold = 0
italic = shape.format.italic
if not italic is not None:
italic = 0
underline = shape['format']['underline']
if underline:
underline = 'sng'
else:
underline = 'none'
strike = shape['format']['font_strikeout']
if strike:
strike = 'Strike'
else:
strike = 'noStrike'
attributes = [
('lang', 'en-US'),
('sz', size),
('b', bold),
('i', italic),
('u', underline),
('strike', strike),
('baseline', 0),
]
self._xml_start_tag('a:rPr', attributes)
color = shape.format.color
if color is not None:
color = shape._get_palette_color(color)
# color =~ s/^FF//; # Remove leading FF from rgb for shape color.
else:
color = '000000'
self._write_a_solid_fill(color)
font = shape.format.font
if font is not None:
font = 'Calibri'
attributes = [('typeface', font)]
self._xml_empty_tag('a:latin', attributes)
self._xml_empty_tag('a:cs', attributes)
self._xml_end_tag('a:rPr')
self._xml_data_element('a:t', shape.text)
self._xml_end_tag('a:r')
self._xml_end_tag('a:p')
self._xml_end_tag('xdr:txBody')
|