Skip to content

Copy paragraphs #28

@BlueAndi

Description

@BlueAndi

How can I copy the paragraphs of one document to another document or e.g. into a table cell?

This was my current code, but always something is missing. Now the bullet list styles and the hyperlinks are gone.

    @staticmethod
    def docx_copy_paragraph(source_paragraph: Paragraph, target_paragraph: Paragraph) -> None:
        """
        Copy the content and formatting from source_paragraph to target_paragraph.

        Args:
            source_paragraph (Paragraph): The source paragraph to copy from.
            target_paragraph (Paragraph): The target paragraph to copy to.
        """
        # Copy each run (text with formatting) from the source paragraph
        # to the target paragraph.
        for run in source_paragraph.runs:
            target_run = target_paragraph.add_run(run.text, run.style)

            source_run_property = run._r.get_or_add_rPr() # pylint: disable=protected-access
            new_run_property = copy.deepcopy(source_run_property)

            target_run._r.replace(target_run._r.get_or_add_rPr(), new_run_property) # pylint: disable=protected-access

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions