1 votes

python-docx does not change font size

Good afternoon I'm trying to generate a .docx file with python-docx, but it does not change the size of the funte this is my code

from docx import Document
from docx.shared import Inches
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
import datetime

def imprimir_ticket():
    doc = Document()
    run = doc.add_paragraph('dsjf').add_run()
    font = run.font
    font.name = 'Calibri'
    font.size = Pt(12)
    doc.add_page_break()
    doc.save('C:/Users/Angel/Desktop/dsjff.docx')
imprimir_ticket()

keeps the font size at 11pt

2voto

FJSevilla Points 29084

In the line run = doc.add_paragraph('dsjf').add_run() indeed run is an object Run (elements <w:r> ) but has no content, the text is added using the initializer of Paragraph .

When adding text to an object Paragraph either at initialization or subsequently by means of the attribute text what happens is that an object is created Run which overwrites the entire contents of the paragraph and to which the text is asynchronized. In your case, you implicitly create an object Run using the initializer of Paragraph with the text and then add another object Run explicitly, without content and to which you modify the source. Keep in mind that the source is a property that belongs to Run not to the Paragraph container.

You could access this object using the attribute runs of the instance of Paragraph and modify the source:

doc = Document()
paragraph = doc.add_paragraph('dsjf')
run = paragraph.runs[0]
font = run.font
font.name = 'Calibri'
font.size = Pt(12)

Now, it is better to simply separate the instantiation of the Paragraph and its Run (this will allow the paragraph to be referred to later without complications) and explicitly add the text through the instance of the Run :

from docx import Document
from docx.shared import Inches
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
import datetime

def imprimir_ticket():
    doc = Document()
    par = doc.add_paragraph()
    run = par.add_run('dsjf')
    font = run.font
    font.name = 'Calibri'
    font.size = Pt(12)
    doc.add_page_break()
    doc.save('C:/Users/Angel/Desktop/dsjff.docx')

imprimir_ticket()

You can define your own custom styles to make it easier if you are going to use and intersperse several styles:

from docx import Document
from docx.shared import Pt
from docx.enum.style import WD_STYLE_TYPE

document = Document()

styles = document.styles
charstyle = styles.add_style('Calibri_12', WD_STYLE_TYPE.CHARACTER)
obj_font = charstyle.font
obj_font.size = Pt(12)
obj_font.name = 'Calibri'

styles.add_style('TNR_15', WD_STYLE_TYPE.CHARACTER)
obj_font = charstyle.font
obj_font.size = Pt(15)
obj_font.name = 'Times New Roman'

paragraph = document.add_paragraph()
paragraph.add_run("Hola Stack Overflow en Español\n", style='Calibri_12')
paragraph.add_run("Adios Stack Overflow en Español", style='TNR_15')
document.add_page_break()
document.save('hola.docx')

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X