Aleksandre Bregadze
Aleksandre Bregadze

Reputation: 25

python docx remove space between text and image?

I think picture will explain everything...

    paragraph3 = doc.add_paragraph(
        text_dict["title_before_graph1"][0],
        style=style_object["competence_" + text_dict["title_before_graph1"][1]],
    )
    paragraph3.alignment = WD_PARAGRAPH_ALIGNMENT.JUSTIFY

    doc.add_picture("results/section3/competence_1.png", width=Inches(6.25))

Amazing right?
When I manually remove the empty line after Georgian school the picture jumps to previous page and it looks wonderful, what should I do? How to remove that empty line?

Upvotes: 0

Views: 332

Answers (1)

Oluwafemi Sule
Oluwafemi Sule

Reputation: 38942

You can check if there are newline characters terminating your paragraph text by printing it in a rawstring format.

print(f'{text_dict["title_before_graph1"][0]!r}')

If you find them at the end of the text, strip them.

    paragraph_text = text_dict["title_before_graph1"][0]
    paragraph_text = paragraph_text.rstrip()
    paragraph3 = doc.add_paragraph(
        paragraph_text,
        style=style_object["competence_" + text_dict["title_before_graph1"][1]],
    )

Upvotes: 1

Related Questions