Search code examples
pythonms-wordpython-docxaspose.words

Python - Insert pandas data frame in between of the document


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.


Solution

  • 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)