How to bold the text of a row cells in python docx?

Viewed 7096

I am generating a doc using python docx module. I want to bold the specific cell of a row in python docx

here is the code

book_title = '\n-:\n {}\n\n'.format(book_title)
book_desc = '-: {}\n\n:\n{}\n\n :\n{}'.format(book.author,book_description,sales_point)


row1.cells[1].text = (book_title + book_desc)

I just want to bold the book_title. If I apply a style it automatically applies to whole document.

4 Answers

A cell does not have a character style; character style can only be applied to text, and in particular to a run of text. This is in fact the defining characteristic of a run, being a sequence of characters that share the same character formatting, also known as font in python-docx.

To get the book title with a different font than the description, they need to appear in separate runs. Assigning to Cell.text (as you have) results in all the text being in a single run.

This might work for you, but assumes the cell is empty as you start:

paragraph = row1.cells[1].paragraphs[0]
title_run = paragraph.add_run(book_title)
description_run = paragraph.add_run(book_desc)
title_run.bold = True

This code can be made more compact:

paragraph = row1.cells[1].paragraphs[0]
paragraph.add_run(book_title).bold = True
paragraph.add_run(book_desc)

but perhaps the former version makes it more clear just what you're doing in each step.

Here is how I understand it: Paragraph is holding the run objects and styles (bold, italic) are methods of run. So following this logic here is what might solve your question:

row1_cells[0].paragraphs[0].add_run(book_title + book_desc).bold=True

This is just an example for the first cell of the table. Please amend it in your code.

Since you are using the docx module, you can style your text/paragraph by explicitly defining the style.

In order to apply a style, use the following code snippet referenced from docx documentation here.

>>> from docx import Document
>>> document = Document()
>>> style = document.styles['Normal']
>>> font = style.font
>>> font.bold= True

This will change the font style to bold for the applied paragraph.

In python-docx, the styling of any character in a docx template document can be overridden by the use of Rich Text styling. You should provide a context variable for the particular character/string that needs styling in your template, at the position of the character/string. This variable maps to the RichText object that has the style definition(that you define in your code), to style the character/string. To make things clearer, consider an example template doc "test.docx" that contains the following text:

Hello {{r context_var}}!

The {{..}} is the jinja2 tag syntax and {{r is the RichText tag that overrides the character styling. The context_var is a variable that maps the styling to your character string. We accomplish Rich Text styling like this:

from docxtpl import DocxTemplate, RichText
doc = DocxTemplate("test.docx")
rt = RichText() #create a RichText object
rt.add('World', bold=True) #pass the text as an argument and the style, bold=True
context = { 'context_var': rt } #add context variable to the context and map it to rt
doc.render(context) #render the context
doc.save("generated_doc.docx") #save as a new document

Let's look at the contents of "generated_doc.docx":

Hello World!

I'm not sure how your template is designed, but if you just want the book_title as bold, your template "test.docx" should have text like:

Title:-

{{r book_title_var}}

The code should be modified to:

book_title = "Lord of the Rings" #or wherever you get the book title from
rt.add(book_title, bold=True)
context = { 'book_title_var': rt }

generated_doc.docx:

Title:-

Lord of the Rings

Related