【问题标题】:Possible to insert row at specific position with python-docx?可以使用 python-docx 在特定位置插入行吗?
【发布时间】:2022-03-03 19:38:14
【问题描述】:

我想使用python-docx 在表格中间插入几行。有什么办法吗?我尝试使用similar to inserting pictures approach,但没有成功。

如果不是,我将不胜感激有关哪个模块更适合此任务的任何提示。谢谢。

这是我尝试模仿插入图片的想法。这是错的。 “Run”对象没有“add_row”属性。

from docx import Document
doc = Document('your docx file')
tables = doc.tables
p = tables[1].rows[4].cells[0].add_paragraph()
r = p.add_run()
r.add_row()
doc.save('test.docx')

【问题讨论】:

  • 显示无效的代码
  • @dotancohen 原来“运行”对象没有属性“add_row”。我已经在问题中添加了代码,但这显然是错误的方式。

标签: python python-docx


【解决方案1】:

简短的回答是否定的。API 中没有 Table.insert_row() 方法。

一种可能的方法是编写一个直接操作底层 XML 的所谓“变通方法”。您可以从它的 python-docx proxy 对象访问任何给定的 XML 元素(例如,在这种情况下为 <w:tbl> 或者可能是 <w:tr>)。例如:

tbl = table._tbl

这为您提供了 XML 层次结构的起点。从那里您可以从头开始创建一个新元素,或者通过复制并使用lxml._Element API 调用将其放置在 XML 中的正确位置。

这是一种高级方法,但可能是最简单的选择。据我所知,没有其他 Python 包可以提供更广泛的 API。另一种方法是在 Windows 中使用他们的 COM API 或来自 VBA 的任何东西,可能是 IronPython。这只适用于运行 Windows 操作系统的小规模(台式机,而不是服务器)。

搜索python-docx workaround functionpython-pptx workaround function 会找到一些示例。

【讨论】:

  • 非常感谢您的详细解释!
【解决方案2】:

虽然根据python-docx文档没有直接可用的api来实现这一点,但有一个简单的解决方案,不使用lxml等任何其他库,只需使用python-docx提供的底层数据结构,即是 CT_Tbl、CT_Row 等。 这些类确实有像 addnext、addprevious 这样的通用方法,它们可以方便地将元素作为兄弟元素添加到当前元素之后/之前。 所以问题可以如下解决,(在python-docx v0.8.10上测试)


    from docx import Document
    doc = Document('your docx file')
    tables = doc.tables
    row = tables[1].rows[4]
    tr = row._tr # this is a CT_Row element
    for new_tr in build_rows(): # build_rows should return list/iterator of CT_Row instance
        tr.addnext(new_tr)
    doc.save('test.docx')

这应该可以解决问题

【讨论】:

    【解决方案3】:

    您可以将行插入表格的末尾,然后将其移动到另一个位置,如下所示:

    from docx import Document
    doc = Document('your docx file')
    t = doc.tables[0]
    row0 = t.rows[0] # for example
    row1 = t.rows[-1]
    row0._tr.addnext(row1._tr)
    

    【讨论】:

      【解决方案4】:

      您可以通过这种方式在最后一个位置添加一行:

      from win32com import client
      doc = word.Documents.Open(r'yourFile.docx'))
      doc = word.ActiveDocument
      table = doc.Tables(1)  #number of the tab you want to manipulate
      table.Rows.Add()
      

      【讨论】:

        【解决方案5】:

        lxml.etree 中的

        addnext() 似乎是更好的选择,并且工作正常,唯一的问题是,我无法设置行的高度,所以请提供一些答案,如果你知道!

        current_row = table.rows[row_index] 
        table.rows[row_index].height_rule = WD_ROW_HEIGHT_RULE.AUTO
        tbl = table._tbl
        border_copied = copy.deepcopy(current_row._tr)
        tr = border_copied
        current_row._tr.addnext(tr)
        

        【讨论】:

          【解决方案6】:

          我在这里制作了一个视频来演示如何做到这一点,因为它第一次让我陷入了循环。 https://www.youtube.com/watch?v=nhReq_0qqVM

              document=Document("MyDocument.docx")
              Table = document.table[0]
              Table.add_row()
              
              for cells in Table.rows[-1].cells:
                   cells.text = "test text"
              
              insertion_row = Table.rows[4]._tr
              insertion_row.add_next(Table.rows[-1]._tr)
              
              document.save("MyDocument.docx")
          

          python-docx 模块没有解决这个问题的方法,所以我发现最好的解决方法是在表格底部创建一个新行,然后使用 xml 元素中的方法将其放置在它的位置应该是的。

          这将创建一个新行,该行中的每个单元格都具有“测试文本”值,然后我们将该行添加到我们的插入行下方。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-07-29
            • 1970-01-01
            相关资源
            最近更新 更多