Alignment is not kept after document merge

When I merge a docx file1, that has a centered text (ParagraphAlignment.CENTER), to another docx file2 that has a justified text (ParagraphAlignment.JUSTIFY), the resulting docx file does not keep the first allignment (center) of the text from file1. The code for merging is below:

import sys
import aspose.words as aw
import aspose.pydrawing as drawing

def merge_docx(filename1, filename2, sub_chapter_name, merged_file):
    try:
        # Load both input documents
        input1 = aw.Document(filename1)
        input2 = aw.Document(filename2)

        # Create a document builder for the destination document
        builder = aw.DocumentBuilder(input1)

        # Move to the end of the document and insert a new line
        builder.move_to_document_end()
        builder.insert_paragraph()

        # Apply style to the paragraph
        builder.paragraph_format.style_name = "Heading 2"
        builder.paragraph_format.alignment = aw.ParagraphAlignment.JUSTIFY
        font = builder.font
        font.size = 16
        font.color = drawing.Color.black
        font.name = "Garamond"
#        builder.font.color = drawing.Color.black

        builder.write(sub_chapter_name)
        builder.writeln()

        # Insert the content of input2 into input1
        builder.insert_document(input2, aw.ImportFormatMode.KEEP_DIFFERENT_STYLES)

        # Save the merged document
        input1.save(merged_file, aw.SaveFormat.DOCX)
        print(f"Merged document saved as: {merged_file}")
    except Exception as e:
        print(f"An error occurred during merging: {str(e)}")
        sys.exit(1)

if __name__ == "__main__":
    if len(sys.argv) != 5:
        print("Usage: python merge_docx.py <filename1> <filename2> <merged_file> <sub_chapter_name>")
        sys.exit(1)
    
    filename1 = sys.argv[1]
    filename2 = sys.argv[2]
    sub_chapter_name = sys.argv[3]
    merged_file = sys.argv[4]
    print(f"Merging {filename2} into {filename1}")
    merge_docx(filename1, filename2, sub_chapter_name, merged_file)

Any help is appreciated.

@ansar2024 Could you please attach your input and output documents here for testing? We will check the issue and provide you more information.

Please find attached the files:
file1.docx (16.2 KB)

file2.docx (17.6 KB)

Thank you.

@ansar2024 Thank you for additional information. As I can see alignment is preserved. Here is output document produced by the following code:
out.docx (15.6 KB)

input1 = aw.Document("C:\\Temp\\file1.docx")
input2 = aw.Document("C:\\Temp\\file2.docx")

# Create a document builder for the destination document
builder = aw.DocumentBuilder(input1)

# Move to the end of the document and insert a new line
builder.move_to_document_end()
builder.insert_paragraph()

# Apply style to the paragraph
builder.paragraph_format.style_name = "Heading 2"
builder.paragraph_format.alignment = aw.ParagraphAlignment.JUSTIFY
font = builder.font
font.size = 16
font.name = "Garamond"

builder.write("This is test title")
builder.writeln()

# Insert the content of input2 into input1
builder.insert_document(input2, aw.ImportFormatMode.KEEP_DIFFERENT_STYLES)

# Save the merged document
input1.save("C:\\Temp\\out.docx", aw.SaveFormat.DOCX)

My bad. Thank you very much for pointing that to me.

1 Like