-1

I need to make this paragraph bold but I am not able to do so I used p.bold = True but it did not work I also used add_run for each line but it is showing that int type and bool type do not have any attribute add_run.

    p = document.add_paragraph(F"""





{doj}

Employee Code: {record[57]}

{record[1]} {record[2]} {record[3]} {record[4]}
{record[12]},
{record[13]},{record[15]}

Dear {record[2]},""")

I tried to do it like this but it did not work

    document = Document()

    p = document.add_paragraph(F"""
{doj}""").bold = True
    p.add_run(F"""Employee Code: {record[57]}""").bold = True
    p.add_run(F"""{record[1]} {record[2]} {record[3]} {record[4]}""").bold = True
    p.add_run(F"""{record[12]},""").bold = True
    p.add_run(F"""{record[13]},{record[15]}""").bold = True

    p.add_run(F"""Dear {record[2]},""").bold = True
Gavya Mehta
  • 211
  • 6
  • 20

1 Answers1

0

Hope This will Solve your issue How do I apply both bold and italics in python-docx?

Explanation provided at https://python-docx.readthedocs.io/en/latest/user/quickstart.html


Run objects have both a .bold and .italic property that allows you to set their value for a run:

paragraph = document.add_paragraph('Lorem ipsum ')
run = paragraph.add_run('dolor')
run.bold = True
paragraph.add_run(' sit amet.')

which produces text that looks like this: ‘Lorem ipsum dolor sit amet.’

Note that you can set bold or italic right on the result of .add_run() if you don’t need it for anything else:

paragraph.add_run('dolor').bold = True

# is equivalent to:

run = paragraph.add_run('dolor')
run.bold = True

# except you don't have a reference to `run` afterward

It’s not necessary to provide text to the .add_paragraph() method. This can make your code simpler if you’re building the paragraph up from runs anyway:

paragraph = document.add_paragraph()
paragraph.add_run('Lorem ipsum ')
paragraph.add_run('dolor').bold = True
paragraph.add_run(' sit amet.')

raza sikander
  • 143
  • 13