from fpdf import FPDF
pdf1 = FPDF
pdf1.multi_cell(0, 5, 'This is my disclaimer. THESE WORDS NEED TO BE BOLD. These words do not need to be bold.', 0, 0, 'L')
pdf1.output("sample.pdf")
Asked
Active
Viewed 1,894 times
3

Gino Mempin
- 25,369
- 29
- 96
- 135

vivek
- 33
- 6
-
what is the question ? – Nishant Nawarkhede Mar 18 '20 at 09:53
-
@NishantNawarkhede i want bold text in between paragraph. – vivek Mar 18 '20 at 10:17
-
You can't do it with multi_cell(). – Olivier Mar 18 '20 at 10:48
3 Answers
3
I use fpdf for php and had the same need. But as Olivier mentioned in the comments it is not possible. So I wrote a quick workaround, I have written a rough python implementation below. Probably not the most beautiful solution but it might serve you.
from fpdf import FPDF
class FPDF_bold_multicell(FPDF):
def write_multicell_with_styles(self,max_width, cell_height, text_list):
startx = self.get_x()
self.set_font('Arial', '', 16)
#loop through differenct sections in different styles
for text_part in text_list:
#check and set style
try:
current_style = text_part['style']
self.set_font('Arial', current_style, 16)
except KeyError:
self.set_font('Arial', '', 16)
#loop through words and write them down
space_width = self.get_string_width(' ')
for word in text_part['text'].split(' '):
current_pos = self.get_x()
word_width = self.get_string_width(word)
#check for newline
if (current_pos + word_width) > (startx + max_width):
#return
self.set_y(self.get_y() + cell_height)
self.set_x(startx)
pdf1.cell(word_width, 5, word)
#add a space
self.set_x(self.get_x() + space_width)
pdf1 = FPDF_bold_multicell()
pdf1.add_page()
text_list = [{'text':'This is my disclaimer.'},
{'style':'B','text':'THESE WORDS NEED TO BE BOLD.'},
{'text':'These words do not need to be bold.'},]
pdf1.write_multicell_with_styles(50, 5, text_list)
pdf1.output("sample.pdf")

Sebastian
- 5,471
- 5
- 35
- 53
0
I've tried this solution and it is helpfull. I added a new command after the comment #check for newline.
#check for newline
if max_width == 0:
max_width = self.w - self.r_margin - self.x
So, this command will verify the width and if was 0 will use the all line.
from fpdf import FPDF
class FPDF_bold_multicell(FPDF):
def write_multicell_with_styles(self,max_width, cell_height, text_list):
startx = self.get_x()
self.set_font('Arial', '', 16)
#loop through differenct sections in different styles
for text_part in text_list:
#check and set style
try:
current_style = text_part['style']
self.set_font('Arial', current_style, 16)
except KeyError:
self.set_font('Arial', '', 16)
#loop through words and write them down
space_width = self.get_string_width(' ')
for word in text_part['text'].split(' '):
current_pos = self.get_x()
word_width = self.get_string_width(word)
#check for newline
if max_width == 0:
max_width = self.w - self.r_margin - self.x
if (current_pos + word_width) > (startx + max_width):
#return
self.set_y(self.get_y() + cell_height)
self.set_x(startx)
pdf1.cell(word_width, 5, word)
#add a space
self.set_x(self.get_x() + space_width)
pdf1 = FPDF_bold_multicell()
pdf1.add_page()
text_list = [{'text':'This is my disclaimer.'},
{'style':'B','text':'THESE WORDS NEED TO BE BOLD.'},
{'text':'These words do not need to be bold.'},]
pdf1.write_multicell_with_styles(0, 5, text_list)
pdf1.output("sample.pdf")
0
i have a paragraph of text in which I have some data inserted from a dictionary, now the data inserted from the dictionary need to be bold, i do have successfully achieved making it bold but the problem I have is that each word is at a new line.
my_dict = {'name':'exo'}
page_text = """
This is permanent text +f'{my_dict["name"] }'"""
for j in range(len(page_text.split())):
if my_dict["name"] == page_text[j]:
pdf.set_font('Arial','BU',12)
else:
pdf.set_font('Arial','',12)
pdf.cell(0,5,page_text[j])
The output:
This
Is
Permanent
Text
Exo #this is bold as I need

aditya thakker
- 104
- 6