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.