• Python3-excel文档操作(三):利用openpyxl库处理excel表格:获取excel表格中的图片信息


    1.简介:

    和word文档一样,Excel文件也可以认为是一个特殊的压缩文件,可以用unzip命令进行解压。

    同理,可以使用openpyxl来获取excel中的图片信息。

    2. 使用unzip命令来解压Excel,从而获取图片:

    cp file_20220729115746.xlsx file_excel_zip.zip. #把原excel文件名改为zip后缀

    unzip file_excel_zip.zip -d file_zip #解压zip文件,结果如下:

    1. Archive: file_excel_zip.zip
    2. inflating: file_zip/docProps/app.xml
    3. inflating: file_zip/docProps/core.xml
    4. inflating: file_zip/xl/theme/theme1.xml
    5. inflating: file_zip/xl/worksheets/sheet1.xml
    6. inflating: file_zip/xl/drawings/drawing1.xml
    7. inflating: file_zip/xl/drawings/_rels/drawing1.xml.rels
    8. inflating: file_zip/xl/worksheets/_rels/sheet1.xml.rels
    9. inflating: file_zip/xl/worksheets/sheet2.xml
    10. inflating: file_zip/xl/media/image1.jpeg
    11. inflating: file_zip/xl/styles.xml
    12. inflating: file_zip/_rels/.rels
    13. inflating: file_zip/xl/workbook.xml
    14. inflating: file_zip/xl/_rels/workbook.xml.rels
    15. inflating: file_zip/[Content_Types].xml

    cd file_zip/xl/media

    % ls

    image1.jpeg

    可见,在file_zip/xl/media目录先,存放着图片文件。

    3. 使用openpyxl来获取excel中的图片信息:

    1. # -*- coding: utf-8 -*-
    2. import os
    3. import sys
    4. import time
    5. import openpyxl
    6. from openpyxl import load_workbook
    7. from openpyxl.drawing.image import Image
    8. def openxls_read_img(fname):
    9. '''
    10. 获取图片信息
    11. '''
    12. wb=load_workbook(fname,data_only=True);
    13. sheet=wb['mysheet1'] #获取sheet
    14. for image in sheet._images:
    15. # 输出图片的位置信息
    16. print(image.anchor._from)
    17. print("image",image.path)
    18. #print("image data",image._data())
    19. if __name__ == '__main__':
    20. #1. case1
    21. # openxls_create();
    22. #2.case2
    23. fname = '人员列表.xlsx';
    24. #openxls_read(fname)
    25. #3.case3
    26. #img_path = "念奴娇_赤壁怀古_image1.jpg";
    27. #openxls_insert_img(fname,img_path)
    28. #4. case4
    29. openxls_read_img("file_20220729115746.xlsx")

    运行结果:

    % python3 openxls_creat_read.py

    Parameters:

    col=3, colOff=0, row=2, rowOff=0

    image /xl/media/image1.jpeg

    说明:

    /xl/media/image1.jpeg 其实就是解压后的图片文件目录

    4. openpyxl.drawing.image源码:

    1. # Copyright (c) 2010-2022 openpyxl
    2. from io import BytesIO
    3. try:
    4. from PIL import Image as PILImage
    5. except ImportError:
    6. PILImage = False
    7. def _import_image(img):
    8. if not PILImage:
    9. raise ImportError('You must install Pillow to fetch image objects')
    10. if not isinstance(img, PILImage.Image):
    11. img = PILImage.open(img)
    12. return img
    13. [docs]class Image(object):
    14. """Image in a spreadsheet"""
    15. _id = 1
    16. _path = "/xl/media/image{0}.{1}"
    17. anchor = "A1"
    18. def __init__(self, img):
    19. self.ref = img
    20. mark_to_close = isinstance(img, str)
    21. image = _import_image(img)
    22. self.width, self.height = image.size
    23. try:
    24. self.format = image.format.lower()
    25. except AttributeError:
    26. self.format = "png"
    27. if mark_to_close:
    28. # PIL instances created for metadata should be closed.
    29. image.close()
    30. def _data(self):
    31. """
    32. Return image data, convert to supported types if necessary
    33. """
    34. img = _import_image(self.ref)
    35. # don't convert these file formats
    36. if self.format in ['gif', 'jpeg', 'png']:
    37. img.fp.seek(0)
    38. fp = img.fp
    39. else:
    40. fp = BytesIO()
    41. img.save(fp, format="png")
    42. fp.seek(0)
    43. data = fp.read()
    44. fp.close()
    45. return data
    46. @property
    47. def path(self):
    48. return self._path.format(self._id, self.format)

    说明:

    1) Image类中的path属性:其实就是解压后的图片文件目录;

    2) Image类中的_data()方法:用于获取图片数据。例如,

    print("image data",image._data()):

    的执行结果是:

    image data b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00H\x00H\x00\x00\xff\xe1\x00pExif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x04\x01\x06\x00\x03\x00\x00\x00\x01\x00\x02\x00\x00\x01\x12\x00\x03\x00\x00\x00\x01\x00\x01\x00\x00\x01(\x00\x03\x00\x00\x00\x01\x00\x02\x00\x00\x87i\x00\x04\x00\x00\x00\x01\x00\x00\x00>\x00\x00\x00\x00\x00\x03\xa0\x01\x00\x03\x00\x00\x00\x01\x00\x01\x00\x00\xa0\x02\x00\x04\x00\x00\x00\x01\x00\x00\x02S\xa0\x03\x00\x04\x00\x00\x00\x01\x00\x00\x01\x83\x00\x00\x00\x00\xff\xed\x008Photoshop 3.0\x008BIM......

  • 相关阅读:
    过滤器 监听器
    图论(一)之概念介绍与图形#matlab
    从手工测试转自动化测试后起薪就20k,原来可以这么简单...
    C# 利用.NET 升级助手将.NET Framework项目升级为.NET 6
    前端基础建设与架构17 学习 axios:封装一个结构清晰的 Fetch 库
    从网站流量指标开始,CSDN 如何洞察运营效果异动?丨评测来了
    Real-Time Rendering——9.13.1 Filtering Normals and Normal Distributions过滤法线和正态分布
    数据结构 - 逻辑结构和物理结构
    MacOS Pro笔记本硬盘升级纪实
    【乐吾乐大屏可视化组态编辑器】使用手册
  • 原文地址:https://blog.csdn.net/liranke/article/details/126063815