使用python-docx对单词表单元格进行着色

时间:2016-12-15 14:27:41

标签: xml python-2.7 python-docx

以下代码用于生成word文档。 我试图用这些代码行

将单元格t2到t7浅灰色
shading_elm = parse_xml(r'<w:shd {} w:fill="D9D9D9"/>'.format(nsdecls('w')))
t2._tc.get_or_add_tcPr().append(shading_elm)

这在一定程度上起作用,但只有最后一个实例才有效。所以只有一个阴影细胞。 我打印出单元格t6和t7的xml代码(参见代码片段的底部),你可以清楚地看到阴影只被插入到x7中的t7(最后一个实例)。 <w:shd w:fill="D9D9D9"/>

我需要遮蔽多个细胞。这可以在不改变单词风格的情况下实现,因为我已经尝试了这个并且它没有给出我想要的结果。

# -*- 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 &amp; EXISTING ENVIRONMENT:</w:t>
    </w:r>
  </w:p>
</w:tc>

1 个答案:

答案 0 :(得分:2)

您必须为要放置它的每个位置创建一个新的shading_elm。否则,同一个地方会从一个地方移动到另一个地方,只会出现在最后一个位置。