How to copy paragraphs (with number formatting) and images from Words (.docx) and paste into Excel (.xlsx) using Python

Beverly Pope countryone77 at gmail.com
Mon Mar 23 14:43:46 EDT 2020


On Mar 22, 2020, at 11:12 PM, A S <aishan0403 at gmail.com> wrote:
> 
> On Monday, 23 March 2020 01:58:38 UTC+8, Beverly Pope  wrote:
>>> On Mar 22, 2020, at 9:47 AM, A S <aishan0403 at gmail.com <mailto:aishan0403 at gmail.com>> wrote:
>>> 
>>> I can't seem to paste pictures into this discussion so please see both my current and desired Excel output here:
>>> 
>>> https://stackoverflow.com/questions/60800494/how-to-copy-paragraphs-with-number-formatting-and-images-from-words-docx-an <https://stackoverflow.com/questions/60800494/how-to-copy-paragraphs-with-number-formatting-and-images-from-words-docx-an><https://stackoverflow.com/questions/60800494/how-to-copy-paragraphs-with-number-formatting-and-images-from-words-docx-an <https://stackoverflow.com/questions/60800494/how-to-copy-paragraphs-with-number-formatting-and-images-from-words-docx-an>>
>> Did you try using the 2 part answer on the stackoverflow webpage?
>> 
>> Bev in TX
> 
> I'm able to get the paragraphs copied correctly now! But i'm trying to figure out if there's a way to copy and paste the images into the Excel, along with the paragraphs as well. Do you have an idea? :)

I’m glad to hear that solution worked for you.  With that said, I only went to stackoverflow out of curiosity and happened ro see the posted solution. I probably know less about using Python to copy data from Word to Excel than you do, given that yesterday was the first time that I had heard about it.  

I did read that MS docx files are zip files, which can be unzipped in Python.
https://gist.github.com/another-junior-dev/990a4e622868627cb93be3d8fa2eff04 <https://gist.github.com/another-junior-dev/990a4e622868627cb93be3d8fa2eff04>
That could provide access to the pictures contained in the document, but it doesn’t explain how to determine where you want to place the pictures with relation to text in your Excel spreadsheet.  If you could determine that, then you could use XlsxWriter module’s worksheet.insert_image() to insert the image.  See n”the Worksheet Class” in the XlsxWriter docs:
https://xlsxwriter.readthedocs.io <https://xlsxwriter.readthedocs.io/>
The rest is beyond the realm of my knowledge.

If it were me, I would go back to stackoveflow and open a new question, as this is different than your original post.

Bev in TX


More information about the Python-list mailing list