Skip to content

Copy paragraphs #28

Open
Open

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

Activity

  1. Skelmis commented on Oct 21, 2025

    @Skelmis
    Owner

    Great question, its likely because one of the features implemented doesnt quite do it the way the library expects and therefore it fails to read it out in the run tag. I can take a look and try some things but no promises

  2. Skelmis commented on Oct 21, 2025

    @Skelmis
    Owner

    This seems like a complicated problem, see here for an example.

    Perhaps also take a look at this? https://github.com/darkvisiontech/docxcompose/tree/master

  3. BlueAndi commented on Oct 21, 2025

    @BlueAndi
    Author

    This seems like a complicated problem, see here for an example.

    Oh yes, it looks like. There in the repository are many other similar issues too. Hmm ... I will re-think about my use case and whether I can workaround the need of copying paragraphs. Murphys law ... everything works, but just this little last piece ... :-)

    Perhaps also take a look at this? https://github.com/darkvisiontech/docxcompose/tree/master

    I will have a look to docxcompose, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions