Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sat Jan 11, 2025 5:32 pm

I want to extract data from a table while preserving everything including text, MathType equations, images, and all other content. Could you help me with that?
I already have the code as shown below, but it does not work as expected.
def copy_table_rows_with_cloned_paragraphs(file_path, output_file):
# Load the source document
doc = Document()
doc.LoadFromFile(file_path)

# Create a new document for output
other_doc = Document()

# Iterate through sections in the document
for section_index in range(doc.Sections.Count):
section = doc.Sections.get_Item(section_index) # Access each section
new_section = other_doc.AddSection() # Add a new section to the new document

# Iterate through tables in the section
for table_index in range(section.Tables.Count):
table = section.Tables.get_Item(table_index) # Access each table in the section

# Iterate through rows in the table
for row_index in range(table.Rows.Count):
row = table.Rows.get_Item(row_index) # Access each row

# Iterate through the cells in the row
for cell_index in range(row.Cells.Count):
cell = row.Cells.get_Item(cell_index) # Access each cell
for para_index in range(cell.Paragraphs.Count):
paragraph = cell.Paragraphs.get_Item(para_index) # Access each paragraph
new_paragraph = new_section.Body.AddParagraph() # Add new paragraph to the new section
# Clone the paragraph (content + format + all internal elements)
new_paragraph = paragraph.Clone() # Clone the entire paragraph (including all internal content)
# Save the new document
other_doc.SaveToFile(output_file)
print(f"Document with cloned paragraphs copied successfully to: {output_file}")
ima.png

nguyennhathuy
 
Posts: 2
Joined: Sat Jan 11, 2025 5:23 pm

Mon Jan 13, 2025 2:56 am

Hello,

Thank you for your inquiry. In order for us to investigate your issue more accurately, please provide your current test input document and the saved result document. Thank you in advance.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1534
Joined: Wed Apr 25, 2018 3:20 am

Wed Jan 15, 2025 6:29 am

Thanks, I did it.

nguyennhathuy
 
Posts: 2
Joined: Sat Jan 11, 2025 5:23 pm

Wed Jan 15, 2025 6:51 am

Hello,

I'm glad you solved it. You also can refer to the following adjusted code. If there are any other issues, please feel free to contact us.
Code: Select all
from spire.doc.common import *
from spire.doc import *

#Original file
doc = Document()
doc.LoadFromFile("test.docx")

# Create a new Document
other_doc = Document()

# Iterate through each section in the source document.
for k in range(doc.Sections.Count):
    sec = doc.Sections.get_Item(k)
    #Add section for "other_doc"
    new_section = other_doc.AddSection()

    # Iterate through each table in the source document.
    for table_index in range(sec.Tables.Count):
        table = sec.Tables.get_Item(table_index)
        for row_index in range(table.Rows.Count):
            row = table.Rows.get_Item(row_index)

            paragraphAdd = Paragraph(other_doc)
            for cell_index in range(row.Cells.Count):
                cell = row.Cells.get_Item(cell_index)


                for para_index in range(cell.Paragraphs.Count):
                    para = cell.Paragraphs.get_Item(para_index)
                    for element_index in range(para.ChildObjects.Count):
                        #Add each row of the table to a paragraph
                        paragraphAdd.ChildObjects.Add(para.ChildObjects.get_Item(element_index).Clone())

            #Add the paragraph to  "other_doc"
            new_section.Body.ChildObjects.Add(paragraphAdd)
# Save the file
other_doc.SaveToFile("out.docx", FileFormat.Docx2013)

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1534
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc