Updating style properties (HEADING1 etc)

Hi,

I can’t find a way to update HEADING1 and NORMAL properties just once at the beginning of the script.
I have to set font size etc each time I use any of the styles.

Is there a way to do that? I have searched for it but couldn’t find a solution.

This is the way I have to do it to get the desired result:

builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING1
builder.font.bold = True
builder.font.size = 16
builder.writeln("Heading 1")

builder.paragraph_format.style_identifier = aw.StyleIdentifier.NORMAL
builder.font.bold = False
builder.font.size = 12
builder.writeln("Text 1")

builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING1
builder.font.bold = True
builder.font.size = 16
builder.writeln("Heading 2")

etc…

Any help would be much appreciated.

@SuneVav You should update styles in the document. Please see the following code:

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

# update styles
heading1 = doc.styles.get_by_style_identifier(aw.StyleIdentifier.HEADING1)
heading1.font.bold = True
heading1.font.size = 16
normal = doc.styles.get_by_style_identifier(aw.StyleIdentifier.NORMAL)
normal.font.bold = False
normal.font.size = 12

builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING1
builder.writeln("Heading 1")

builder.paragraph_format.style_identifier = aw.StyleIdentifier.NORMAL
builder.writeln("Text 1")

builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING1
builder.writeln("Heading 2")

doc.save("C:\\Temp\\out.docx")

Thanks Alexey,

I don’t get any errors but it doesn’t quite seem to work.
I get all text in bold and size 14. It looks like the default setting.

The style settings I set don’t seem to get applied correctly.

@SuneVav The code works fine on my side. Here is the document produced on my side:
out.docx (7.1 KB)

Ok, thanks!
I’ll try with an empty script tomorrow to make sure I didn’t miss something in my code.

1 Like

Hi,

I just wanted to let you know that it works perfectly fine if I only run this test code and create a new document.
So I will troubleshoot my code and perhaps also the Word template I’m using.
Thanks again!

@SuneVav Please feel free to ask if you need more assistance from us. We are always glad to help you.

1 Like