question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

run.add_picture() won't insert the image into the document

See original GitHub issue

I’m trying to editing an existing Word document (.docx). Everything works well and I can easily add images at the end of the document with : document.add_picture("my_picture.png") .

My problem is presented when I try to get a specific paragraph and add a picture inside a new run.

ex. document.paragraphs[20].add_run().add_picture("my_picture.png")

The command executes correctly, but then when I open the saved word document no image is present at the paragraph n.20.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
scannycommented, Jul 29, 2021

Post the before-repair and after-repair XML and let’s have a look:

paragraph = document.paragraphs[20]
run = paragraph.add_run()
picture = run.add_picture("my_picture.png")
print(paragraph._p.xml)

Then after the repair:

paragraph = document.paragraphs[20]
print(paragraph._p.xml)
0reactions
muddybeast1commented, Dec 7, 2021

I was able to cope up with the error by not using paragraph spaces before and after. So, I think there is a problem in either line_spacing or space_after methods. Adding to that I was also not able to add picture using document.add_picture() call as stated in issue.

paragraph_2 = document.add_paragraph()
paragraph_2_run_1 = paragraph_2.add_run('Test Graphs: ')
paragraph_2_run_1.bold = True
paragraph_2_format = paragraph_2.paragraph_format
# paragraph_2_format.line_spacing = Cm(2)
# paragraph_2_format.space_after = Cm(1)

paragraph_3 = document.add_paragraph()
paragraph_3_run_1 = paragraph_3.add_run()
paragraph_3_run_1.add_picture("Test_Graph1.png")

When adding picture forcefully in word application it appeared to misbehave and inserted picture under text. After that, saving the document generates error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Apache Poi: adding an image to paragraph does not work
The result is that if I add both image and title, then it generates an empty document. If I add only the title,...
Read more >
49765 – addPictures() not displaying Image in XWPF
When I use the method addPictures() for XWPF documents, I assume this adds the image data to the Word document without any additional...
Read more >
Cannot insert Pictures into Word, PowerPoint, Excel
I have installed OpenOffice and inserted the same test picture into a Documents, Spreadsheet and Presentation (in Open Office) and the image ......
Read more >
Adding Images to a Word Document using Java
Java makes it possible to add images to Word documents using the addPicture() method of XWPFRun class provided by Apache POI package.
Read more >
Include a Picture or Other Image in Your Sheet
You can insert images directly into cells in your sheets. Images in the grid can be useful for many types of sheets, including...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found