python-从excel中提取图表,导出图片

python-从excel中提取图表,导出图片

from win32com.client import Dispatch
import os
import logging
import traceback
import sys

"""
从excel中提取图表,生成图片
"""

def excelChart(workbook_file_name):
    """


    :param workbook_file_name:
    :param dataFormat: 需要格式化的数据
    :return:
    """


    # workbook_file_name = r'D:\pythonworkspace\develop\DCU\prime\scan\analyse\data.xlsx'
    try:
        app = Dispatch("Excel.Application")
        workbook = app.Workbooks.Open(Filename=workbook_file_name)
        app.DisplayAlerts = False
        imageList = []
        for sheet in workbook.Worksheets:
            for chartObject in sheet.ChartObjects():
                imagePath = os.path.abspath("./image")
                if not os.path.exists(imagePath):
                    os.makedirs(imagePath)
                imageFilePath = f"{os.path.join(imagePath, sheet.Name+'.png')}"
                chartObject.Chart.Export(imageFilePath)
                imageList.append(imageFilePath)
        return imageList
    except Exception as er:
        logging.error(f"** Meet exception ** :{er}")
        logging.error("".join(traceback.format_exception(*sys.exc_info())))
    finally:
        workbook.Close(SaveChanges=True, Filename=workbook_file_name)

你可能感兴趣的:(win32com,python)