python - 使用 Python-docx 更新表格的单元格内容

标签 python python-docx

我正在使用 python 2.7 Python-docx 尝试修改 Microsoft docx 文档中表格中单元格的内容。我以 xml 格式打开文档,以便查看内容所在的位置并尝试获取值以便我可以引用它们。以下是我发现的内容。

<w:tbl>
<w:tblPr>
   <w:tblStyle w:val="TableGrid"/>
   <w:tblW w:w="0" w:type="auto"/>
   <w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>
</w:tblPr>
<w:tblGrid>
   <w:gridCol w:w="1345"/>
   <w:gridCol w:w="3148"/>
   <w:gridCol w:w="3148"/>
   <w:gridCol w:w="3149"/>
</w:tblGrid>
<w:tr w:rsidR="002C543C" w14:paraId="4C33FE0D" w14:textId="77777777" w:rsidTr="009E290C">
  <w:trPr>
    <w:cantSplit/>
    <w:trHeight w:hRule="exact" w:val="1080"/>
  </w:trPr>
  <w:tc>
    <w:tcPr>
      <w:tcW w:w="1345" w:type="dxa"/>
    </w:tcPr>
    <w:p w14:paraId="4497FDDB" w14:textId="77777777" w:rsidR="002C543C" w:rsidRDefault="002C543C">
       <w:pPr>
           <w:rPr>
             <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
             <w:sz w:val="24"/>
             <w:szCs w:val="24"/>
           </w:rPr>
      </w:pPr>
    </w:p>
  </w:tc>
  <w:tc>
     <w:tcPr>
        <w:tcW w:w="3148" w:type="dxa"/>
     </w:tcPr>
     <w:p w14:paraId="15F285F0" w14:textId="77777777" w:rsidR="002C543C" w:rsidRDefault="002C543C" w:rsidP="009E290C">
        <w:pPr>
        <w:jc w:val="center"/>
        <w:rPr>
        <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
       <w:sz w:val="24"/>
     <w:szCs w:val="24"/>
     </w:rPr>
   </w:pPr>
   </w:p>
   <w:p w14:paraId="140917B0" w14:textId="77777777" w:rsidR="009E290C" w:rsidRPr="001261E4" w:rsidRDefault="009E290C" w:rsidP="009E290C">
    <w:pPr>
    <w:jc w:val="center"/>
      <w:rPr>
       <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
      <w:b/>
      <w:sz w:val="24"/>
      <w:szCs w:val="24"/>
     </w:rPr>
     </w:pPr>
     <w:r w:rsidRPr="001261E4">
  <w:rPr>
  <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
  <w:b/>
  <w:sz w:val="24"/>
  <w:szCs w:val="24"/>
  </w:rPr>
  <w:t>this is cell (1, 2)</w:t>
  </w:r>
  </w:p>

所以使用上面的 xml 作为下面的引用是我尝试实现更新单元格 (0,0) 的方法。

from docx import Document
from docx.shared import Inches
from docx.oxml.table import CT_Tbl
from docx.oxml.text.paragraph import CT_P
from docx.table import Table
from docx.text.paragraph import Paragraph


   f = open('filename.docx')
   doc = Document(f)
   table_to_update = Table('04A0', doc) # value from above <w:tblLook w:val="04A0"
   cell = table_to_update.cell(0, 0) # this produces the error 
   #cell.text = 'can we add something'

此代码产生以下错误

AttributeError: 'str' object has no attribute 'col_count'

所以我假设它来 self 发送的 04A0 值。所以首先我试图找到如何引用我想要修改的表。从那里我想找到该表中的单元格并对其进行修改。我一直在寻找这方面的例子,但一直找不到。

最佳答案

Table的构造函数接受 <w:tbl> XML 子树而不是表的字符串 id(这就是它失败的原因)。此外,您认为 id 是 actually :

Specifies what aspects of the table styles should be included. This is a bitmask of options: 0x0020=Apply header row formatting; 0x0040=Apply last row formatting; 0x0080=Apply header column formatting; 0x0100=Apply last column formatting.


您可以使用以下代码获取文档中的表格列表:

doc = Document('filename.docx')
print(doc.tables)

然后你必须明白,你需要修改哪些表格(通过列表中的位置或表格的标题或任何适用的)。为简单起见,我将使用第一个表。当你有你Table对象,您可以通过执行以下操作修改单元格值:

table = doc.tables[0]
table.cell(0, 0).text = 'new value'

然后您可以保存更新的文档:

doc.save('filename_updated.docx')

关于python - 使用 Python-docx 更新表格的单元格内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34519746/

相关文章:

python - 无法从网页中抓取标题

python - Pandas 数据框 : error joining

python - Python中的NameError - 无法调用函数

python - 系列的真值不明确 - Python-Docx

python - 使用 docx.Document() 时的回溯(最近一次调用最后一次)

python - 将15小时长的音频文件拆分为一小时长的文件

python - 如何在 Python for 循环中使用多个条件?

python - 如何在 python-docx 中应用粗体和居中?

Python-Docx 将 HTML 插入 Docx

python - 使用 docx 模块使用 Python 检查 .docx 形式的复选框