0

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.

macropod
  • 12,757
  • 2
  • 9
  • 21
Ajay
  • 783
  • 3
  • 16
  • 37
  • This question ([Add an image in a specific position in the document (.docx)?](https://stackoverflow.com/q/32932230/5358968)) addresses how to put a picture between text instead of just at the end - the same answers might carry over to your case? – Steve Jun 09 '22 at 12:53
  • Tried above approach, getting error message `table = r.add_table(rows=2, cols=3) AttributeError: 'Run' object has no attribute 'add_table'` – Ajay Jun 09 '22 at 13:00

1 Answers1

1

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)
Alexey Noskov
  • 1,722
  • 1
  • 7
  • 13
  • I have to add bookmark in the existing document. Consider above word example, bookmark is required in "insert table here". When I am using your code snippet, it is adding table in top of the document. – Ajay Jun 10 '22 at 06:40
  • Awesome.. this is working fine. I have another requirement to copy content from another document and paste in specific position of a document, keeping the formatting intact. Suppose, we want to insert content of another document in place of "insert text here" and then create a table. – Ajay Jun 10 '22 at 08:53
  • 1
    you can achieve this using DocumentBuilder.insert_document method. See the updated answer. – Alexey Noskov Jun 10 '22 at 09:05