可以使用 python-docx 在特定位置插入行吗?
Possible to insert row at specific position with python-docx?
我想使用 python-docx
在 table 中间插入几行。有什么办法吗?我试过使用 但没用。
如果没有,我将不胜感激关于哪个模块更适合这项任务的任何提示。谢谢
这是我尝试模仿插入图片的想法。这是错的。 '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')
简短的回答是否定的。API 中没有 Table.insert_row()
方法。
一种可能的方法是编写一个所谓的 "workaround function" 来直接操纵底层 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 OS.
搜索 python-docx workaround function
和 python-pptx workaround function
会找到一些示例。
您可以通过这种方式在最后位置添加一行:
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()
尽管根据 python-docx 文档没有直接可用的 api 来实现此目的,但有一个简单的解决方案,无需使用任何其他库(例如 lxml),只需使用python-docx提供的底层数据结构,有CT_Tbl、CT_Row等。
这些 类 确实有 addnext、addprevious 等常用方法,可以方便地将元素添加为兄弟元素 after/before 当前元素。
所以问题可以解决如下,(在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')
这应该可以解决问题
您可以将行插入到 table 的末尾,然后将其移动到另一个位置,如下所示:
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)
addnext() in lxml.etree 似乎是更好的选择,它工作正常,唯一的问题是,我无法设置行的高度,所以请提供一些答案,如果你知道!
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)
我在这里创建了一个视频来演示如何执行此操作,因为它第一次让我陷入了循环。
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 模块没有这个方法,所以我找到的最好的解决方法是在 table 的底部创建一个新行,然后使用 xml 个元素,将其放置在预期的位置。
这将创建一个新行,该行中的每个单元格都具有值“test text”,然后我们将该行添加到 insertion_row.
下方
我想使用 python-docx
在 table 中间插入几行。有什么办法吗?我试过使用
如果没有,我将不胜感激关于哪个模块更适合这项任务的任何提示。谢谢
这是我尝试模仿插入图片的想法。这是错的。 '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')
简短的回答是否定的。API 中没有 Table.insert_row()
方法。
一种可能的方法是编写一个所谓的 "workaround function" 来直接操纵底层 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 OS.
搜索 python-docx workaround function
和 python-pptx workaround function
会找到一些示例。
您可以通过这种方式在最后位置添加一行:
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()
尽管根据 python-docx 文档没有直接可用的 api 来实现此目的,但有一个简单的解决方案,无需使用任何其他库(例如 lxml),只需使用python-docx提供的底层数据结构,有CT_Tbl、CT_Row等。 这些 类 确实有 addnext、addprevious 等常用方法,可以方便地将元素添加为兄弟元素 after/before 当前元素。 所以问题可以解决如下,(在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')
这应该可以解决问题
您可以将行插入到 table 的末尾,然后将其移动到另一个位置,如下所示:
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)
addnext() in lxml.etree 似乎是更好的选择,它工作正常,唯一的问题是,我无法设置行的高度,所以请提供一些答案,如果你知道!
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)
我在这里创建了一个视频来演示如何执行此操作,因为它第一次让我陷入了循环。 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 模块没有这个方法,所以我找到的最好的解决方法是在 table 的底部创建一个新行,然后使用 xml 个元素,将其放置在预期的位置。
这将创建一个新行,该行中的每个单元格都具有值“test text”,然后我们将该行添加到 insertion_row.
下方