Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script to save image from xlsx file to disk

Tags:

python

I do have an excel worksheet which has various images included inside. I want to store those images as PNG or JPEG files on the disk. Can anyone suggest if there is any solution to achieve this?

I have tried with python xlrd module but it ignores images inside xlsx.

like image 313
Abhishek Kulkarni Avatar asked Mar 03 '26 12:03

Abhishek Kulkarni


1 Answers

Going off of @DSM's comment, this chunk of code works to extract embedded jpeg or jpg files from an xlsx file. They will end up in the directory from which you run the program, nested in the folders that are shown in the original archive contents:

import zipfile
XLSname = "/Users/user/myfile.xlsx"

EmbeddedFiles = zipfile.ZipFile(XLSname).namelist()
ImageFiles = [F for F in EmbeddedFiles if F.count('.jpg') or F.count('.jpeg') ]

for Image in ImageFiles:
    zipfile.ZipFile(XLSname).extract(Image)
like image 173
beroe Avatar answered Mar 06 '26 03:03

beroe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!