Python script to save image from xlsx file to disk

2020-07-22 16:31发布

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.

标签: python
1条回答
家丑人穷心不美
2楼-- · 2020-07-22 17:11

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)
查看更多
登录 后发表回答