question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature: set table cell background color

See original GitHub issue

Hi…

I’m trying to add the ability in my code to set the background colour of the cell. This should be done similar to the font colours, by adding a solidFill etc, so I wrote:

def set_cell_background_color(cell, rgbColor):
   print "cell %r" % cell
   print "color %s" % rgbColor
   tc = cell._Cell__tc
   solidFill = _SubElement(tc, 'a:solidFill')
   srgbClr = _SubElement(solidFill, 'a:srgbClr')
   srgbClr.set('val', rgbColor)

Which I guessed as being correct after looking in shapes.py at _Cell, and seeing where it put the marL etc attributes. This ends up creating an empty tcPr element, followed by the solidFill, which Powerpoint doesn’t like.

It seems that _Cell does, somehow/somewhere, create an empty tcPr element, but I can’t see how to add my solidFill to it. If I change my code to:

def set_cell_background_color(cell, rgbColor):
   print "cell %r" % cell
   print "color %s" % rgbColor
   tc = cell._Cell__tc
   solidFill = _SubElement(tc, 'a:solidFill')
   srgbClr = _SubElement(solidFill, 'a:srgbClr')
   srgbClr.set('val', rgbColor)

Then I get TWO tcPr elements, which isn’t allowed.

Any ideas?

(Issue completely rewritten as I was being a nitwit…)

From the relaxng spec: a_CT_TableCell =

default value: 1

attribute rowSpan { xsd:int }?,

default value: 1

attribute gridSpan { xsd:int }?,

default value: false

attribute hMerge { xsd:boolean }?,

default value: false

attribute vMerge { xsd:boolean }?, attribute id { xsd:string }?, element txBody { a_CT_TextBody }?, element tcPr { a_CT_TableCellProperties }?, element extLst { a_CT_OfficeArtExtensionList }?

a_CT_TableCellProperties =

default value: 91440

attribute marL { a_ST_Coordinate32 }?,

default value: 91440

attribute marR { a_ST_Coordinate32 }?,

default value: 45720

attribute marT { a_ST_Coordinate32 }?,

default value: 45720

attribute marB { a_ST_Coordinate32 }?,

default value: horz

attribute vert { a_ST_TextVerticalType }?,

default value: t

attribute anchor { a_ST_TextAnchoringType }?,

default value: false

attribute anchorCtr { xsd:boolean }?,

default value: clip

attribute horzOverflow { a_ST_TextHorzOverflowType }?, element lnL { a_CT_LineProperties }?, element lnR { a_CT_LineProperties }?, element lnT { a_CT_LineProperties }?, element lnB { a_CT_LineProperties }?, element lnTlToBr { a_CT_LineProperties }?, element lnBlToTr { a_CT_LineProperties }?, element cell3D { a_CT_Cell3D }?, a_EG_FillProperties?, element headers { a_CT_Headers }?, element extLst { a_CT_OfficeArtExtensionList }?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
scannycommented, Dec 19, 2013

Here’s a specific snippet for a table cell:

from pptx.dml.color import RGBColor

# cell is a table cell
# set fill type to solid color first
cell.fill.solid()

# set foreground (fill) color to a specific RGB color
cell.fill.fore_color.rgb = RGBColor(0xFB, 0x8F, 0x00)
1reaction
scannycommented, Dec 8, 2013

This feature was added with the latest commit on develop: c5e0fba9ba81046f07ccb590c088bb15925e0426

Usage is the same as for shape, substituting cell where shape appears in this sample code

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add or change the fill color of a table cell - Microsoft Support
Add or change a fill color. Select the cells in which you want to add or change the fill color. On the Table...
Read more >
How to Easily Set the Table Background Color
To change the color of a row, you can add the “style” property into the <td> brackets and define the color of the...
Read more >
[FEATURE-13817]Change Table Background Color
Here's an example of a table in a carousel object that would otherwise show that ugly white space below the rows with data....
Read more >
How to change background color of cell in table using java script
Try this: function btnClick() { var x = document.getElementById("mytable").getElementsByTagName("td"); x[0].innerHTML = "i want to change my ...
Read more >
SI Editor Table Menu - Set Background Color - SpecsIntact
Column · To change the Background Color for a Column, begin by placing the cursor over any cell within the row · Right-click...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found