Python-docx: Bold type and style into a cell

Created on 21 Oct 2015  路  7Comments  路  Source: python-openxml/python-docx

Hello, I'm new in this, I want to set one word with bold type and Arial font and the rest of words with Helvetica font, all the string with size of "8" (Arial bold and Normal Helvetica, All those words into one cell), thank for your help.

By the way I have one more question, how can I set the cell with border?

Below is one picture about I want Achieve.

Regards.

issue

Most helpful comment

@carlosmtz2106 even though your post is from nearly a year ago, if you are still wondering how to accomplish such a task you can do something like the following.

Nombre_text = 'Nombre del Documento: '
table = document.add_table(rows=1, cols=1, style='Table Grid')
row = table.rows[0]
Nombre_text_formatted = row.cells[0].paragraphs[0].add_run(Nombre_text)
Nombre_text_formatted.font.name = 'Arial'
Nombre_text_formatted.font.size = Pt(8)
Nombre_text_formatted.bold = True
other_text = 'Plantilla de recoleccion de evidencias de la GSO "RedHat Enterprise Linux 5"'
other_text_formatted = row.cells[0].paragraphs[0].add_run(other_text)
other_text_formatted.font.name = 'Helvetica'
other_text_formatted.font.size = Pt(8)

Remember to import docx as well as docx.shared for the font size in Pts. Hopefully that helps and if you have any questions just let me know.

All 7 comments

@carlosmtz2106 even though your post is from nearly a year ago, if you are still wondering how to accomplish such a task you can do something like the following.

Nombre_text = 'Nombre del Documento: '
table = document.add_table(rows=1, cols=1, style='Table Grid')
row = table.rows[0]
Nombre_text_formatted = row.cells[0].paragraphs[0].add_run(Nombre_text)
Nombre_text_formatted.font.name = 'Arial'
Nombre_text_formatted.font.size = Pt(8)
Nombre_text_formatted.bold = True
other_text = 'Plantilla de recoleccion de evidencias de la GSO "RedHat Enterprise Linux 5"'
other_text_formatted = row.cells[0].paragraphs[0].add_run(other_text)
other_text_formatted.font.name = 'Helvetica'
other_text_formatted.font.size = Pt(8)

Remember to import docx as well as docx.shared for the font size in Pts. Hopefully that helps and if you have any questions just let me know.

This was very helpful in my python-docx project. Thx.

Very helpful indeed. Thank you!

You're welcome @spssoftie and @smanol

I'm glad I was able to help out!

@Sum4196
From your example here I see how to apply the font to one cell.
I am looking for an elegant way to apply a font size for whole the table at once.
-- a lot of thanks --

@baruchgu I havent used the library since I made that comment, but from what I remember the style is applied to a cell object and not the table (I may be wrong on this, please refer to the documentation). That said, there may be a more elegant way to apply the styles than I posted above if you want the whole table styled a certain way. Doing a quick Google search I found this link which may be more of what you're looking for.
https://stackoverflow.com/questions/43051462/python-docx-how-to-set-cell-width-in-tables#43053996

@Sum4196
I want to only modify the table font size and already found it:
table = document.add_table(rows=1, cols=5, style='Light List Accent 1')
table.style.font.size = Pt(9)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

scanny picture scanny  路  8Comments

chcourses picture chcourses  路  3Comments

Tores76 picture Tores76  路  6Comments

madphysicist picture madphysicist  路  3Comments

sskadit picture sskadit  路  4Comments