Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Aug 05, 2024 8:50 pm

Hi guys,

I am wanted to change the font/size/etc of a heading (in python) - I know how to create custom styles using ParagraphStyle(), but I don't know how to set this to be a heading.

Currently I am just using the default "BuiltinStyle.Heading1" but our company requires specific fonts for their documents, and so I need to change this style (or create a custom one) to match

Cheers

Duncanator
 
Posts: 4
Joined: Mon Aug 05, 2024 8:46 pm

Tue Aug 06, 2024 3:47 am

Hi,

Thank you four your inquiry.
Please refer to code below to change the font of heading style. If there's still any issue, welcome to contact us again.

Code: Select all
from spire.doc import *
from spire.doc.common import *

outputFile = "Styles.docx"
#Initialize a document
document = Document()
sec = document.AddSection()

#Add default heading1 style
heading1Style = document.AddStyle(BuiltinStyle.Heading1)

heading1Style.CharacterFormat.FontName = "cambria"
heading1Style.CharacterFormat.FontSize = 14

heading1Style.CharacterFormat.Bold = True
heading1Style.CharacterFormat.TextColor = Color.FromArgb(255, 42, 123, 136)

paragraph = sec.AddParagraph()
paragraph.AppendText("Objective")
paragraph.ApplyStyle(BuiltinStyle.Heading1)

#Save to docx file.
document.SaveToFile(outputFile, FileFormat.Docx)
document.Close()


Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 460
Joined: Mon Nov 07, 2022 8:10 am

Return to Spire.Doc

cron