Paste the styles after sentence correction

Hi we have a document which contains grammar corrections and sentence corrections

we have run information along with actual paragraph - stored in form of dictionary, which contains complete paragraph as ‘text’ and style(font name, font size, font color, bold, italic, underline, font background, font shading, superscript, subscript, hyperlink) for each run for paragraph

Corrected text - we have corrected string/paragraph with words inserted/deleted, which changes the length of the actual text

now based on the corrected text, we want to paste the corrected text back to its original place with all styles intact

we have run information along with actual paragraph - stored in form of dictionary, which contains complete paragraph as ‘text’ and style(font name, font size, font color, bold, italic, underline, font background, font shading, superscript, subscript, hyperlink) for each run for paragraph

Corrected text - we have corrected string/paragraph with words inserted/deleted, which changes the length of the actual text

now based on the corrected text, we want to paste the corrected text back to its original place with all styles intact

[{'type': 'paragraph',
  'para_id': 1,
  'text': 'The Bankruptcy procedure applies to all companies are that have their docile or placeof buiness in Somecountry.',
  'runs': [{'text': 'The Bankruptcy procedure applies to all ',
    'bold': False,
    'italic': False,
    'underline': '0',
    'font_name': 'Calibri',
    'font_size': 11.0,
    'color': None,
    'highlight_color': None,
    'shading_color': None,
    'all_caps': False,
    'strike_through': False,
    'superscript': False,
    'subscript': False,
    'hyperlink': False,
    'hyperlink_url': None},
   {'text': 'companies ',
    'bold': False,
    'italic': False,
    'underline': '0',
    'font_name': 'Calibri',
    'font_size': 11.0,
    'color': None,
    'highlight_color': None,
    'shading_color': None,
    'all_caps': False,
    'strike_through': False,
    'superscript': False,
    'subscript': False,
    'hyperlink': False,
    'hyperlink_url': None},
   {'text': 'are that ',
    'bold': False,
    'italic': False,
    'underline': '0',
    'font_name': 'Calibri',
    'font_size': 11.0,
    'color': None,
    'highlight_color': (255, 255, 0),
    'shading_color': None,
    'all_caps': False,
    'strike_through': False,
    'superscript': False,
    'subscript': False,
    'hyperlink': False,
    'hyperlink_url': None},
   {'text': 'have',
    'bold': True,
    'italic': False,
    'underline': '0',
    'font_name': 'Calibri',
    'font_size': 11.0,
    'color': None,
    'highlight_color': (255, 255, 0),
    'shading_color': None,
    'all_caps': False,
    'strike_through': False,
    'superscript': False,
    'subscript': False,
    'hyperlink': False,
    'hyperlink_url': None},
   {'text': ' their ',
    'bold': False,
    'italic': False,
    'underline': '0',
    'font_name': 'Calibri',
    'font_size': 11.0,
    'color': None,
    'highlight_color': (255, 255, 0),
    'shading_color': None,
    'all_caps': False,
    'strike_through': False,
    'superscript': False,
    'subscript': False,
    'hyperlink': False,
    'hyperlink_url': None},
   {'text': 'docile',
    'bold': True,
    'italic': True,
    'underline': '1',
    'font_name': 'Calibri',
    'font_size': 11.0,
    'color': None,
    'highlight_color': (255, 255, 0),
    'shading_color': None,
    'all_caps': False,
    'strike_through': False,
    'superscript': False,
    'subscript': False,
    'hyperlink': False,
    'hyperlink_url': None},
   {'text': ' or ',
    'bold': False,
    'italic': False,
    'underline': '0',
    'font_name': 'Calibri',
    'font_size': 11.0,
    'color': None,
    'highlight_color': (255, 255, 0),
    'shading_color': None,
    'all_caps': False,
    'strike_through': False,
    'superscript': False,
    'subscript': False,
    'hyperlink': False,
    'hyperlink_url': None},
   {'text': 'place',
    'bold': True,
    'italic': False,
    'underline': '0',
    'font_name': 'Calibri',
    'font_size': 11.0,
    'color': (112, 48, 160),
    'highlight_color': (255, 255, 0),
    'shading_color': None,
    'all_caps': False,
    'strike_through': False,
    'superscript': False,
    'subscript': False,
    'hyperlink': False,
    'hyperlink_url': None},
   {'text': 'of ',
    'bold': False,
    'italic': False,
    'underline': '0',
    'font_name': 'Calibri',
    'font_size': 11.0,
    'color': None,
    'highlight_color': None,
    'shading_color': None,
    'all_caps': False,
    'strike_through': False,
    'superscript': False,
    'subscript': False,
    'hyperlink': False,
    'hyperlink_url': None},
   {'text': 'buiness',
    'bold': False,
    'italic': False,
    'underline': '0',
    'font_name': 'Calibri',
    'font_size': 11.0,
    'color': None,
    'highlight_color': None,
    'shading_color': None,
    'all_caps': False,
    'strike_through': False,
    'superscript': False,
    'subscript': False,
    'hyperlink': False,
    'hyperlink_url': None},
   {'text': ' in ',
    'bold': False,
    'italic': False,
    'underline': '0',
    'font_name': 'Calibri',
    'font_size': 11.0,
    'color': None,
    'highlight_color': None,
    'shading_color': None,
    'all_caps': False,
    'strike_through': False,
    'superscript': False,
    'subscript': False,
    'hyperlink': False,
    'hyperlink_url': None},
   {'text': 'Somecountry',
    'bold': False,
    'italic': False,
    'underline': '0',
    'font_name': 'Calibri',
    'font_size': 11.0,
    'color': None,
    'highlight_color': None,
    'shading_color': None,
    'all_caps': False,
    'strike_through': False,
    'superscript': False,
    'subscript': False,
    'hyperlink': False,
    'hyperlink_url': None},
   {'text': '.',
    'bold': False,
    'italic': False,
    'underline': '0',
    'font_name': 'Calibri',
    'font_size': 11.0,
    'color': None,
    'highlight_color': None,
    'shading_color': None,
    'all_caps': False,
    'strike_through': False,
    'superscript': False,
    'subscript': False,
    'hyperlink': False,
    'hyperlink_url': None} ]

corrected sentence is as follows
“The bankruptcy procedure applies to all companies that have their domicile or place of business in Somecountry.”
need somehelp in pasting back correct styles based on the dictionary created

@Prakruth You can try the following method:

doc = aw.Document()

paragraph_data = {
    "type": "paragraph",
    "para_id": 1,
    "text": "The Bankruptcy procedure applies...",
    "runs": [
        {
            "text": "The Bankruptcy procedure applies to all ",
            "bold": False,
            "italic": False,
            "underline": "0",
            "font_name": "Calibri",
            "font_size": 11.0,
            "color": None,
            "highlight_color": None,
            "shading_color": None,
            "all_caps": False,
            "strike_through": False,
            "superscript": False,
            "subscript": False,
            "hyperlink": False,
            "hyperlink_url": None,
            # other properties
        },
        # Add all your run data here.
    ]
}

paragraph = aw.Paragraph(doc)

# Process each run in the paragraph data.
for run_data in paragraph_data["runs"]:
    run = aw.Run(doc, run_data["text"])
    self.apply_run_formatting(run, run_data)
    paragraph.append_child(run)

doc.first_section.body.append_child(paragraph)

doc.save("output.docx")

def apply_run_formatting(self, run, run_data) -> None:
    """
    Applies formatting to a run based on the input data.
    """
    font = run.font

    font.name = run_data["font_name"]
    font.size = run_data["font_size"]
    font.bold = run_data["bold"]
    font.italic = run_data["italic"]

    font.underline = aw.Underline.SINGLE if run_data["underline"] == "1" else aw.Underline.NONE

    if run_data["color"] is not None:
        r, g, b = run_data["color"]
        font.color = aw.Color.from_argb(r, g, b)

    if run_data["highlight_color"] is not None:
        r, g, b = run_data["highlight_color"]
        font.highlight_color = aw.Color.from_argb(r, g, b)

    font.all_caps = run_data["all_caps"]
    font.strike_through = run_data["strike_through"]
    font.superscript = run_data["superscript"]
    font.subscript = run_data["subscript"]

    if run_data["hyperlink"] and run_data["hyperlink_url"]:
        # This would require more complex handling with Hyperlink class.
        pass

thanks for the response,
the original text with runs is “The Bankruptcy procedure applies to all companies are that have their docile or placeof buiness in Somecountry.”

the corrected text is “The bankruptcy procedure applies to all companies that have their domicile or place of business in Somecountry.” on to which we need to apply same styles from original text

@Prakruth This is the full data:

paragraph_data = {
    'type': 'paragraph',
    'para_id': 1,
    'text': 'The bankruptcy procedure applies to all companies that have their domicile or place of business in Somecountry.',
    'runs': [
        {
            'text': 'The bankruptcy procedure applies to all ',
            'bold': False,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': None,
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        },
        {
            'text': 'companies ',
            'bold': False,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': None,
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        },
        {
            'text': 'that ',
            'bold': False,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': (255, 255, 0),
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        },
        {
            'text': 'have',
            'bold': True,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': (255, 255, 0),
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        },
        {
            'text': ' their ',
            'bold': False,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': (255, 255, 0),
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        },
        {
            'text': 'domicile',
            'bold': True,
            'italic': True,
            'underline': '1',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': (255, 255, 0),
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        },
        {
            'text': ' or ',
            'bold': False,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': (255, 255, 0),
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        },
        {
            'text': 'place',
            'bold': True,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': (112, 48, 160),
            'highlight_color': (255, 255, 0),
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        },
        {
            'text': ' of ',
            'bold': False,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': None,
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        },
        {
            'text': 'business',
            'bold': False,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': None,
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        },
        {
            'text': ' in ',
            'bold': False,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': None,
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        },
        {
            'text': 'Somecountry',
            'bold': False,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': None,
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        },
        {
            'text': '.',
            'bold': False,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': None,
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        }
    ]
}

if there is a “wikipeda” which is clickable to “www.wikipedia.org” how can we replicate the clickable solution for this? maintaining same styles?

@Prakruth The best way is to work with DocumentBuilder to insert text and hyperlinks. Here is the updated code as an example:

doc = aw.Document()
builder = aw.DocumentBuilder(doc)

paragraph_data = {
    'type': 'paragraph',
    'para_id': 1,
    'text': 'The bankruptcy procedure applies to all companies that have their domicile or place of business in Somecountry.',
    'runs': [
        {
            'text': 'The bankruptcy procedure applies to all ',
            'bold': False,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': None,
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        },
        {
            'text': 'companies ',
            'bold': False,
            'italic': False,
            'underline': '1',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': (0, 0, 255),
            'highlight_color': None,
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': True,
            'hyperlink_url': "www.wikipedia.org"
        },
        {
            'text': 'that have their domicile or place of business in Somecountry.',
            'bold': False,
            'italic': False,
            'underline': '0',
            'font_name': 'Calibri',
            'font_size': 11.0,
            'color': None,
            'highlight_color': None,
            'shading_color': None,
            'all_caps': False,
            'strike_through': False,
            'superscript': False,
            'subscript': False,
            'hyperlink': False,
            'hyperlink_url': None
        }
    ]
}

builder.move_to_document_end()
self.add_formatted_paragraph(builder, paragraph_data)
doc.save("output.docx")

def add_formatted_paragraph(self, builder, paragraph_data):
    # Start a new paragraph
    builder.writeln()  # Ensures we're in a new paragraph
    builder.paragraph_format.style_identifier = aw.StyleIdentifier.NORMAL

    # Process each run in the paragraph
    for run_data in paragraph_data['runs']:
        # Apply run formatting
        font = builder.font
        font.name = run_data['font_name']
        font.size = run_data['font_size']
        font.bold = run_data['bold']
        font.italic = run_data['italic']
        font.underline = aw.Underline.SINGLE if run_data['underline'] == '1' else aw.Underline.NONE
        font.strike_through = run_data['strike_through']
        font.superscript = run_data['superscript']
        font.subscript = run_data['subscript']
        font.all_caps = run_data['all_caps']

        if run_data['color']:
            font.color = aspose.pydrawing.Color.from_argb(255, run_data['color'][0], run_data['color'][1], run_data['color'][2])

        # Handle hyperlink if present
        if run_data['hyperlink'] and run_data['hyperlink_url']:
            # Ensure URL has proper protocol
            url = run_data['hyperlink_url']
            if not url.startswith(('http://', 'https://')):
                url = 'https://' + url

            # Insert hyperlink with the same formatting
            builder.insert_hyperlink(run_data['text'], url, False)
        else:
            # Insert regular text
            builder.write(run_data['text'])

        # Reset formatting after the paragraph
        builder.font.clear_formatting()