python-文档之间插入熊猫的数据框

发布于 2025-02-05 18:47:23 字数 368 浏览 2 评论 0原文

我有一个pandas数据框架,我想在Word Document之间插入它。假设 -

  • 第1节

这是测试Word文档

在此处插入表

  • 第2节

应在第2节之前插入表。

表。应在此处插入PANDAS数据框架。

我检查了python-docx,我们可以很容易地插入表,但是该表将在文档的末尾插入。

我们可以使用Python-docx在文档之间插入表。如果没有,有人可以建议我可以用来实现此功能的其他库。

I have a pandas data frame which I want to insert in between of a word document. Let's say -

  • Section 1

This is test word document

insert table here

  • Section 2

Table should be inserted before section 2.

Pandas data frame should be inserted in place of insert table here.

I checked python-docx, we can insert table very easily but that table will be inserted at the end of a document.

Can we insert table in between of the document using python-docx. If not, can someone suggest other library which I can use to achieve this functionality.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚风撩人 2025-02-12 18:47:23

您可以使用aspose.words轻松实现此目标。在您的情况下,您可以将书签插入占位符,您需要在其中插入表格,然后使用docudmenbuilder在书签上插入表。例如,请参见以下简单代码:

import aspose.words as aw

# Move cursor to the bookmark
builder.move_to_bookmark("table")

# build a table
builder.start_table()
for i in range(5):
    for j in range(5):
        builder.insert_cell()
        builder.write("Cell {0},{1}".format(i, j))
    builder.end_row()
builder.end_table()

doc.save("C:\\Temp\\out.docx")

aspose.words for python文档,以了解有关使用书签与桌子一起工作

更新:如果您需要使用文本作为占位持有人,则可以使用以下代码:

import aspose.words as aw

doc = aw.Document("C:\\Temp\\in.docx")
builder = aw.DocumentBuilder(doc)

# Search for a placeholder paragraph
paragraphs = doc.get_child_nodes(aw.NodeType.PARAGRAPH, True)
for para in paragraphs :
    paraText = para.to_string(aw.SaveFormat.TEXT).strip()
    if paraText == "insert table here":
        # Move cursor to the paragraph
        builder.move_to(para)
        # build a table
        builder.start_table()
        for i in range(5):
            for j in range(5):
                builder.insert_cell()
                builder.write("Cell {0},{1}".format(i, j))
            builder.end_row()
        builder.end_table()

        # If required you can remove the placeholder paragraph.
        para.remove()

# Save the result
doc.save("C:\\Temp\\out.docx")

在.net和Java版本的Aspose.Words。您可以使用ireplacingCallback来实现这一目标,但在Python版本中此功能尚不可用。 irePlacingCallback允许执行自定义操作。

除表外,您可以插入另一个文档的内容,只需使用documentBuilder.insert_document方法即可。代码看起来像这样:

# Move cursor to the paragrapg
builder.move_to(para)
# Insert conten of another document
builder.insert_document(aw.Document("C:\\Temp\\src.docx"),  aw.ImportFormatMode.KEEP_SOURCE_FORMATTING)

You can easily achieve this using Aspose.Words. In your case, you can insert bookmark as a placeholder where you need to insert a table and then use DocumentBuilder to insert table at the bookmark. For example see the following simple code:

import aspose.words as aw

# Move cursor to the bookmark
builder.move_to_bookmark("table")

# build a table
builder.start_table()
for i in range(5):
    for j in range(5):
        builder.insert_cell()
        builder.write("Cell {0},{1}".format(i, j))
    builder.end_row()
builder.end_table()

doc.save("C:\\Temp\\out.docx")

Ass Aspose.Words for Python documentation to learn more about working with bookmarks and working with tables.

UPDATE: If you need to use a text as a placeholder, you can use code like the following:

import aspose.words as aw

doc = aw.Document("C:\\Temp\\in.docx")
builder = aw.DocumentBuilder(doc)

# Search for a placeholder paragraph
paragraphs = doc.get_child_nodes(aw.NodeType.PARAGRAPH, True)
for para in paragraphs :
    paraText = para.to_string(aw.SaveFormat.TEXT).strip()
    if paraText == "insert table here":
        # Move cursor to the paragraph
        builder.move_to(para)
        # build a table
        builder.start_table()
        for i in range(5):
            for j in range(5):
                builder.insert_cell()
                builder.write("Cell {0},{1}".format(i, j))
            builder.end_row()
        builder.end_table()

        # If required you can remove the placeholder paragraph.
        para.remove()

# Save the result
doc.save("C:\\Temp\\out.docx")

In .NET and Java version of Aspose.Words you can use IReplacingCallback to achieve this, but in Python version this feature is not available yet. IReplacingCallback allows to perform a custom actions when Range.Replace action is performed.

Except table, you can insert content of another document, simply use DocumentBuilder.insert_document method. Code will look like this:

# Move cursor to the paragrapg
builder.move_to(para)
# Insert conten of another document
builder.insert_document(aw.Document("C:\\Temp\\src.docx"),  aw.ImportFormatMode.KEEP_SOURCE_FORMATTING)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文