/usr/lib/python2.7/dist-packages/xlsxwriter/styles.py is in python-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 | ###############################################################################
#
# Styles - A class for writing the Excel XLSX Worksheet file.
#
# Copyright 2013, John McNamara, jmcnamara@cpan.org
#
# Package imports.
from . import xmlwriter
class Styles(xmlwriter.XMLwriter):
"""
A class for writing the Excel XLSX Styles file.
"""
###########################################################################
#
# Public API.
#
###########################################################################
def __init__(self):
"""
Constructor.
"""
super(Styles, self).__init__()
self.xf_formats = []
self.palette = []
self.font_count = 0
self.num_format_count = 0
self.border_count = 0
self.fill_count = 0
self.custom_colors = []
self.dxf_formats = []
###########################################################################
#
# Private API.
#
###########################################################################
def _assemble_xml_file(self):
# Assemble and write the XML file.
# Write the XML declaration.
self._xml_declaration()
# Add the style sheet.
self._write_style_sheet()
# Write the number formats.
self._write_num_fmts()
# Write the fonts.
self._write_fonts()
# Write the fills.
self._write_fills()
# Write the borders element.
self._write_borders()
# Write the cellStyleXfs element.
self._write_cell_style_xfs()
# Write the cellXfs element.
self._write_cell_xfs()
# Write the cellStyles element.
self._write_cell_styles()
# Write the dxfs element.
self._write_dxfs()
# Write the tableStyles element.
self._write_table_styles()
# Write the colors element.
self._write_colors()
# Close the style sheet tag.
self._xml_end_tag('styleSheet')
# Close the file.
self._xml_close()
def _set_style_properties(self, properties):
# Pass in the Format objects and other properties used in the styles.
self.xf_formats = properties[0]
self.palette = properties[1]
self.font_count = properties[2]
self.num_format_count = properties[3]
self.border_count = properties[4]
self.fill_count = properties[5]
self.custom_colors = properties[6]
self.dxf_formats = properties[7]
def _get_palette_color(self, color):
# Convert the RGB color.
if color[0] == '#':
color = color[1:]
return "FF" + color.upper()
###########################################################################
#
# XML methods.
#
###########################################################################
def _write_style_sheet(self):
# Write the <styleSheet> element.
xmlns = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'
attributes = [('xmlns', xmlns)]
self._xml_start_tag('styleSheet', attributes)
def _write_num_fmts(self):
# Write the <numFmts> element.
if not self.num_format_count:
return
attributes = [('count', self.num_format_count)]
self._xml_start_tag('numFmts', attributes)
# Write the numFmts elements.
for xf_format in self.xf_formats:
# Ignore built-in number formats, i.e., < 164.
if xf_format.num_format_index >= 164:
self._write_num_fmt(xf_format.num_format_index,
xf_format.num_format)
self._xml_end_tag('numFmts')
def _write_num_fmt(self, num_fmt_id, format_code):
# Write the <numFmt> element.
format_codes = {
0: 'General',
1: '0',
2: '0.00',
3: '#,##0',
4: '#,##0.00',
5: '($#,##0_);($#,##0)',
6: '($#,##0_);[Red]($#,##0)',
7: '($#,##0.00_);($#,##0.00)',
8: '($#,##0.00_);[Red]($#,##0.00)',
9: '0%',
10: '0.00%',
11: '0.00E+00',
12: '# ?/?',
13: '# ??/??',
14: 'm/d/yy',
15: 'd-mmm-yy',
16: 'd-mmm',
17: 'mmm-yy',
18: 'h:mm AM/PM',
19: 'h:mm:ss AM/PM',
20: 'h:mm',
21: 'h:mm:ss',
22: 'm/d/yy h:mm',
37: '(#,##0_);(#,##0)',
38: '(#,##0_);[Red](#,##0)',
39: '(#,##0.00_);(#,##0.00)',
40: '(#,##0.00_);[Red](#,##0.00)',
41: '_(* #,##0_);_(* (#,##0);_(* "-"_);_(_)',
42: '_($* #,##0_);_($* (#,##0);_($* "-"_);_(_)',
43: '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(_)',
44: '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(_)',
45: 'mm:ss',
46: '[h]:mm:ss',
47: 'mm:ss.0',
48: '##0.0E+0',
49: '@'}
# Set the format code for built-in number formats.
if num_fmt_id < 164:
if num_fmt_id in format_codes:
format_code = format_codes[num_fmt_id]
else:
format_code = 'General'
attributes = [
('numFmtId', num_fmt_id),
('formatCode', format_code),
]
self._xml_empty_tag('numFmt', attributes)
def _write_fonts(self):
# Write the <fonts> element.
attributes = [('count', self.font_count)]
self._xml_start_tag('fonts', attributes)
# Write the font elements for xf_format objects that have them.
for xf_format in self.xf_formats:
if xf_format.has_font:
self._write_font(xf_format)
self._xml_end_tag('fonts')
def _write_font(self, xf_format, is_dxf_format=False):
# Write the <font> element.
self._xml_start_tag('font')
# The condense and extend elements are mainly used in dxf formats.
if xf_format.font_condense:
self._write_condense()
if xf_format.font_extend:
self._write_extend()
if xf_format.bold:
self._xml_empty_tag('b')
if xf_format.italic:
self._xml_empty_tag('i')
if xf_format.font_strikeout:
self._xml_empty_tag('strike')
if xf_format.font_outline:
self._xml_empty_tag('outline')
if xf_format.font_shadow:
self._xml_empty_tag('shadow')
# Handle the underline variants.
if xf_format.underline:
self._write_underline(xf_format.underline)
if xf_format.font_script == 1:
self._write_vert_align('superscript')
if xf_format.font_script == 2:
self._write_vert_align('subscript')
if not is_dxf_format:
self._xml_empty_tag('sz', [('val', xf_format.font_size)])
if xf_format.theme:
self._write_color('theme', xf_format.theme)
elif xf_format.color_indexed:
self._write_color('indexed', xf_format.color_indexed)
elif xf_format.font_color:
color = self._get_palette_color(xf_format.font_color)
self._write_color('rgb', color)
elif not is_dxf_format:
self._write_color('theme', 1)
if not is_dxf_format:
self._xml_empty_tag('name', [('val', xf_format.font_name)])
self._xml_empty_tag('family', [('val', xf_format.font_family)])
if xf_format.font_name == 'Calibri' and not xf_format.hyperlink:
self._xml_empty_tag(
'scheme',
[('val', xf_format.font_scheme)])
self._xml_end_tag('font')
def _write_underline(self, underline):
# Write the underline font element.
if underline == 2:
attributes = [('val', 'double')]
elif underline == 33:
attributes = [('val', 'singleAccounting')]
elif underline == 34:
attributes = [('val', 'doubleAccounting')]
else:
# Default to single underline.
attributes = []
self._xml_empty_tag('u', attributes)
def _write_vert_align(self, val):
# Write the <vertAlign> font sub-element.
attributes = [('val', val)]
self._xml_empty_tag('vertAlign', attributes)
def _write_color(self, name, value):
# Write the <color> element.
attributes = [(name, value)]
self._xml_empty_tag('color', attributes)
def _write_fills(self):
# Write the <fills> element.
attributes = [('count', self.fill_count)]
self._xml_start_tag('fills', attributes)
# Write the default fill element.
self._write_default_fill('none')
self._write_default_fill('gray125')
# Write the fill elements for xf_format objects that have them.
for xf_format in self.xf_formats:
if xf_format.has_fill:
self._write_fill(xf_format)
self._xml_end_tag('fills')
def _write_default_fill(self, pattern_type):
# Write the <fill> element for the default fills.
self._xml_start_tag('fill')
self._xml_empty_tag('patternFill', [('patternType', pattern_type)])
self._xml_end_tag('fill')
def _write_fill(self, xf_format, is_dxf_format=False):
# Write the <fill> element.
pattern = xf_format.pattern
bg_color = xf_format.bg_color
fg_color = xf_format.fg_color
# Colors for dxf formats are handled differently from normal formats
# since the normal xf_format reverses the meaning of BG and FG for
# solid fills.
if is_dxf_format:
bg_color = xf_format.dxf_bg_color
fg_color = xf_format.dxf_fg_color
patterns = (
'none',
'solid',
'mediumGray',
'darkGray',
'lightGray',
'darkHorizontal',
'darkVertical',
'darkDown',
'darkUp',
'darkGrid',
'darkTrellis',
'lightHorizontal',
'lightVertical',
'lightDown',
'lightUp',
'lightGrid',
'lightTrellis',
'gray125',
'gray0625',
)
self._xml_start_tag('fill')
# The "none" pattern is handled differently for dxf formats.
if is_dxf_format and pattern <= 1:
self._xml_start_tag('patternFill')
else:
self._xml_start_tag(
'patternFill',
[('patternType', patterns[pattern])])
if fg_color:
fg_color = self._get_palette_color(fg_color)
self._xml_empty_tag('fgColor', [('rgb', fg_color)])
if bg_color:
bg_color = self._get_palette_color(bg_color)
self._xml_empty_tag('bgColor', [('rgb', bg_color)])
else:
if not is_dxf_format:
self._xml_empty_tag('bgColor', [('indexed', 64)])
self._xml_end_tag('patternFill')
self._xml_end_tag('fill')
def _write_borders(self):
# Write the <borders> element.
attributes = [('count', self.border_count)]
self._xml_start_tag('borders', attributes)
# Write the border elements for xf_format objects that have them.
for xf_format in self.xf_formats:
if xf_format.has_border:
self._write_border(xf_format)
self._xml_end_tag('borders')
def _write_border(self, xf_format, is_dxf_format=False):
# Write the <border> element.
attributes = []
# Diagonal borders add attributes to the <border> element.
if xf_format.diag_type == 1:
attributes.append(('diagonalUp', 1))
elif xf_format.diag_type == 2:
attributes.append(('diagonalDown', 1))
elif xf_format.diag_type == 3:
attributes.append(('diagonalUp', 1))
attributes.append(('diagonalDown', 1))
# Ensure that a default diag border is set if the diag type is set.
if xf_format.diag_type and not xf_format.diag_border:
xf_format.diag_border = 1
# Write the start border tag.
self._xml_start_tag('border', attributes)
# Write the <border> sub elements.
self._write_sub_border(
'left',
xf_format.left,
xf_format.left_color)
self._write_sub_border(
'right',
xf_format.right,
xf_format.right_color)
self._write_sub_border(
'top',
xf_format.top,
xf_format.top_color)
self._write_sub_border(
'bottom',
xf_format.bottom,
xf_format.bottom_color)
# Condition DXF formats don't allow diagonal borders.
if not is_dxf_format:
self._write_sub_border(
'diagonal',
xf_format.diag_border,
xf_format.diag_color)
if is_dxf_format:
self._write_sub_border('vertical', None, None)
self._write_sub_border('horizontal', None, None)
self._xml_end_tag('border')
def _write_sub_border(self, border_type, style, color):
# Write the <border> sub elements such as <right>, <top>, etc.
attributes = []
if not style:
self._xml_empty_tag(border_type)
return
border_styles = (
'none',
'thin',
'medium',
'dashed',
'dotted',
'thick',
'double',
'hair',
'mediumDashed',
'dashDot',
'mediumDashDot',
'dashDotDot',
'mediumDashDotDot',
'slantDashDot',
)
attributes.append(('style', border_styles[style]))
self._xml_start_tag(border_type, attributes)
if color:
color = self._get_palette_color(color)
self._xml_empty_tag('color', [('rgb', color)])
else:
self._xml_empty_tag('color', [('auto', 1)])
self._xml_end_tag(border_type)
def _write_cell_style_xfs(self):
# Write the <cellStyleXfs> element.
attributes = [('count', 1)]
self._xml_start_tag('cellStyleXfs', attributes)
self._write_style_xf()
self._xml_end_tag('cellStyleXfs')
def _write_cell_xfs(self):
# Write the <cellXfs> element.
formats = self.xf_formats
# Workaround for when the last xf_format is used for the comment font
# and shouldn't be used for cellXfs.
last_format = formats[-1]
if last_format.font_only:
formats.pop()
attributes = [('count', len(formats))]
self._xml_start_tag('cellXfs', attributes)
# Write the xf elements.
for xf_format in formats:
self._write_xf(xf_format)
self._xml_end_tag('cellXfs')
def _write_style_xf(self):
# Write the style <xf> element.
num_fmt_id = 0
font_id = 0
fill_id = 0
border_id = 0
attributes = [
('numFmtId', num_fmt_id),
('fontId', font_id),
('fillId', fill_id),
('borderId', border_id),
]
self._xml_empty_tag('xf', attributes)
def _write_xf(self, xf_format):
# Write the <xf> element.
num_fmt_id = xf_format.num_format_index
font_id = xf_format.font_index
fill_id = xf_format.fill_index
border_id = xf_format.border_index
xf_id = 0
has_align = 0
has_protect = 0
attributes = [
('numFmtId', num_fmt_id),
('fontId', font_id),
('fillId', fill_id),
('borderId', border_id),
('xfId', xf_id),
]
if xf_format.num_format_index > 0:
attributes.append(('applyNumberFormat', 1))
# Add applyFont attribute if XF format uses a font element.
if xf_format.font_index > 0:
attributes.append(('applyFont', 1))
# Add applyFill attribute if XF format uses a fill element.
if xf_format.fill_index > 0:
attributes.append(('applyFill', 1))
# Add applyBorder attribute if XF format uses a border element.
if xf_format.border_index > 0:
attributes.append(('applyBorder', 1))
# Check if XF format has alignment properties set.
(apply_align, align) = xf_format._get_align_properties()
# Check if an alignment sub-element should be written.
if apply_align and align:
has_align = 1
# We can also have applyAlignment without a sub-element.
if apply_align:
attributes.append(('applyAlignment', 1))
# Check for cell protection properties.
protection = xf_format._get_protection_properties()
if protection:
attributes.append(('applyProtection', 1))
has_protect = 1
# Write XF with sub-elements if required.
if has_align or has_protect:
self._xml_start_tag('xf', attributes)
if has_align:
self._xml_empty_tag('alignment', align)
if has_protect:
self._xml_empty_tag('protection', protection)
self._xml_end_tag('xf')
else:
self._xml_empty_tag('xf', attributes)
def _write_cell_styles(self):
# Write the <cellStyles> element.
attributes = [('count', 1)]
self._xml_start_tag('cellStyles', attributes)
self._write_cell_style()
self._xml_end_tag('cellStyles')
def _write_cell_style(self):
# Write the <cellStyle> element.
name = 'Normal'
xf_id = 0
builtin_id = 0
attributes = [
('name', name),
('xfId', xf_id),
('builtinId', builtin_id),
]
self._xml_empty_tag('cellStyle', attributes)
def _write_dxfs(self):
# Write the <dxfs> element.
formats = self.dxf_formats
count = len(formats)
attributes = [('count', len(formats))]
if count:
self._xml_start_tag('dxfs', attributes)
# Write the font elements for xf_format objects that have them.
for xf_format in self.dxf_formats:
self._xml_start_tag('dxf')
if xf_format.has_dxf_font:
self._write_font(xf_format, True)
if xf_format.num_format_index:
self._write_num_fmt(xf_format.num_format_index,
xf_format.num_format)
if xf_format.has_dxf_fill:
self._write_fill(xf_format, True)
if xf_format.has_dxf_border:
self._write_border(xf_format, True)
self._xml_end_tag('dxf')
self._xml_end_tag('dxfs')
else:
self._xml_empty_tag('dxfs', attributes)
def _write_table_styles(self):
# Write the <tableStyles> element.
count = 0
default_table_style = 'TableStyleMedium9'
default_pivot_style = 'PivotStyleLight16'
attributes = [
('count', count),
('defaultTableStyle', default_table_style),
('defaultPivotStyle', default_pivot_style),
]
self._xml_empty_tag('tableStyles', attributes)
def _write_colors(self):
# Write the <colors> element.
custom_colors = self.custom_colors
if not custom_colors:
return
self._xml_start_tag('colors')
self._write_mru_colors(custom_colors)
self._xml_end_tag('colors')
def _write_mru_colors(self, custom_colors):
# Write the <mruColors> element for the most recently used colours.
# Write the custom custom_colors in reverse order.
custom_colors.reverse()
# Limit the mruColors to the last 10.
if len(custom_colors) > 10:
custom_colors = custom_colors[0:10]
self._xml_start_tag('mruColors')
# Write the custom custom_colors in reverse order.
for color in custom_colors:
self._write_color('rgb', color)
self._xml_end_tag('mruColors')
def _write_condense(self):
# Write the <condense> element.
attributes = [('val', 0)]
self._xml_empty_tag('condense', attributes)
def _write_extend(self):
# Write the <extend> element.
attributes = [('val', 0)]
self._xml_empty_tag('extend', attributes)
|