Shade a word table cell using python-docx
<p>The code below is used to generate a word document.
I have attempted to shade cell t2 through t7 light grey with these lines of code </p>

<pre><code>shading_elm = parse_xml(r'&lt;w:shd {} w:fill="D9D9D9"/&gt;'.format(nsdecls('w')))
t2._tc.get_or_add_tcPr().append(shading_elm)
</code></pre>

<p>This works to a certain extent but only the last instance actually works.  So only one cell in shaded.
I printed out the xml code for cell t6 and t7 (see bottom of code snippet), and you can clearly see that the shading has only been inserted into the xml for t7 (last instance). <code>&lt;w:shd w:fill="D9D9D9"/&gt;</code></p>

<p>I need to shade multiple cells.  Can this be achieved without changing styles in word as I've tried this and it did not give the results I was lokking for.</p>

<pre><code># -*- coding: utf-8 -*-
from docx import Document
from docx.shared import Inches
from docx.enum.text import *

document = Document()

from docx.oxml.ns import nsdecls
from docx.oxml import parse_xml

# Set a cell background (shading) color to RGB D9D9D9.
shading_elm = parse_xml(r'&lt;w:shd {} w:fill="D9D9D9"/&gt;'.format(nsdecls('w')))
#

sections = document.sections
for section in sections:
    #section.top_margin = (margin)
    #section.bottom_margin = (margin)
    section.left_margin = (Inches(.5))
    section.right_margin = (Inches(.5))



document.add_picture('LOGOtrans.png', width = Inches(1.75))
paragraph_containing_new_picture = document.paragraphs[-1]
paragraph_containing_new_picture.alignment = WD_ALIGN_PARAGRAPH.CENTER
document.add_paragraph('HEALTH &amp; SAFETY METHOD STATEMENT')
document.paragraphs[-1].alignment = WD_ALIGN_PARAGRAPH.CENTER

table1 = document.add_table(1,1)
table1.style = 'Table Grid'

t1 = table1.cell(0,0)
t1.add_paragraph("CONTRACT DETAILS").alignment=WD_ALIGN_PARAGRAPH.CENTER


table2 = document.add_table(5,2)
table2.style = 'Table Grid'

t2 = table2.cell(0,0)
t2._tc.get_or_add_tcPr().append(shading_elm)
t3 = table2.cell(1,0)
t3._tc.get_or_add_tcPr().append(shading_elm)
t4 = table2.cell(2,0)
t4._tc.get_or_add_tcPr().append(shading_elm)
t5 = table2.cell(3,0)
t5._tc.get_or_add_tcPr().append(shading_elm)
t6 = table2.cell(4,0)
t6._tc.get_or_add_tcPr().append(shading_elm)


t2.add_paragraph("Site Address:").alignment=WD_ALIGN_PARAGRAPH.LEFT
t3.add_paragraph("Prepared by:").alignment=WD_ALIGN_PARAGRAPH.LEFT
t4.add_paragraph("Date:").alignment=WD_ALIGN_PARAGRAPH.LEFT
t5.add_paragraph("Start Date of Works:").alignment=WD_ALIGN_PARAGRAPH.LEFT
t6.add_paragraph("Contract Duration:").alignment=WD_ALIGN_PARAGRAPH.LEFT

table3 = document.add_table(1,1)
table3.style = 'Table Grid'
t7 = table3.cell(0,0)
t7._tc.get_or_add_tcPr().append(shading_elm)
t7.add_paragraph("SITE LOCATION &amp; EXISTING ENVIRONMENT:").alignment=WD_ALIGN_PARAGRAPH.CENTER


print('XML cell 6: %s' %t6._tc.xml)
print('XML cell 7: %s' %t7._tc.xml)

document.save('demo.docx')



XML cell 6: &lt;w:tc xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"&gt;
  &lt;w:tcPr&gt;
    &lt;w:tcW w:type="dxa" w:w="5400"/&gt;
  &lt;/w:tcPr&gt;
  &lt;w:p/&gt;
  &lt;w:p&gt;
    &lt;w:pPr&gt;
      &lt;w:jc w:val="left"/&gt;
    &lt;/w:pPr&gt;
    &lt;w:r&gt;
      &lt;w:t&gt;Contract Duration:&lt;/w:t&gt;
    &lt;/w:r&gt;
  &lt;/w:p&gt;
&lt;/w:tc&gt;

XML cell 7: &lt;w:tc xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"&gt;
  &lt;w:tcPr&gt;
    &lt;w:tcW w:type="dxa" w:w="10800"/&gt;
    &lt;w:shd w:fill="D9D9D9"/&gt;
  &lt;/w:tcPr&gt;
  &lt;w:p/&gt;
  &lt;w:p&gt;
    &lt;w:pPr&gt;
      &lt;w:jc w:val="center"/&gt;
    &lt;/w:pPr&gt;
    &lt;w:r&gt;
      &lt;w:t&gt;SITE LOCATION &amp;amp; EXISTING ENVIRONMENT:&lt;/w:t&gt;
    &lt;/w:r&gt;
  &lt;/w:p&gt;
&lt;/w:tc&gt;
</code></pre>