Skip to content

copy styles between Word documents - can we have add_style(Style object)? #508

@mieczyslaw

Description

@mieczyslaw

I am trying to copy some styles (various types: paragraph, character, list and table) between Word documents. I am attaching the fragments of the code below. I can read styles from the source document with:

source_document = Document(source_file)
source_styles = source_document.styles

then I am opening the target document and I would like to copy some of the styles there (I can filter myself by style name property):

target_document = Document(full_path)
target_styles = target_document.styles
for s_name in ALLOWED_STYLES:
   if s_name in target_styles:
      target_styles[s_name].delete()
   source_style = source_styles[s_name]
   style = target_styles.add_style(s_name, source_style.type)
   style = source_styles[s_name] # does not work as expected, the style definition is not copied
   #target_document.styles[s_name] = source_styles[s_name] # this is not allowed at all
target_document.save(full_path)

If I run the code above, the styles are added but they don't have the correct properties. It looks that Style objects cannot be assigned. One solution would be to add the second version of add_style(), which would accept the Style object. Alternatively I know that you can set the properties of the style, but I don't know how to iterate over all properties of the source styles to copy them between Style objects.

Thanks for your help

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