Reputation: 473
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
shading_elm = parse_xml(r'<w:shd {} w:fill="D9D9D9"/>'.format(nsdecls('w')))
t2._tc.get_or_add_tcPr().append(shading_elm)
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). <w:shd w:fill="D9D9D9"/>
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.
# -*- 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'<w:shd {} w:fill="D9D9D9"/>'.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 & 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 & 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: <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">
<w:tcPr>
<w:tcW w:type="dxa" w:w="5400"/>
</w:tcPr>
<w:p/>
<w:p>
<w:pPr>
<w:jc w:val="left"/>
</w:pPr>
<w:r>
<w:t>Contract Duration:</w:t>
</w:r>
</w:p>
</w:tc>
XML cell 7: <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">
<w:tcPr>
<w:tcW w:type="dxa" w:w="10800"/>
<w:shd w:fill="D9D9D9"/>
</w:tcPr>
<w:p/>
<w:p>
<w:pPr>
<w:jc w:val="center"/>
</w:pPr>
<w:r>
<w:t>SITE LOCATION & EXISTING ENVIRONMENT:</w:t>
</w:r>
</w:p>
</w:tc>
Upvotes: 1
Views: 2579
Reputation: 28863
You have to create a new shading_elm for each location you want to place it. Otherwise the same one gets moved from one place to another and only appears in the last position.
Upvotes: 4