0
下面的代码用于生成一个单词文档。 我试图遮阳细胞t2到t7的浅灰色的码使用python-docx渲染一个单词表单元格
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代码(请参阅代码片段的底部),并且您可以清楚地看到阴影仅插入t7(最后一个实例)的xml中。 <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 & EXISTING ENVIRONMENT:</w:t>
</w:r>
</w:p>
</w:tc>
非常感谢您的光临。 – Finchy70