辉煌仪奇 2021-11-05 10:04 采纳率: 47.4%
浏览 98
已结题

如何将图片放入excel指定单元格中

下面函数,想要将图片放入指定的excel表格
如何放置

def createtabletoimg(rows: int = 18, cols: int = 9, spec: int = 1, colors: list = []):
    """
    传入excel 以spec分割列,填充颜色,不够列数不够厚等所有列均填充完成后再由上到下填充spec个单元格颜色,读取填充好的excel 将其转换为图片
    :param rows: excel行数
    :param cols: excel列数
    :param spec: 每列多少单元格同一颜色
    :param colors: 填充颜色
    :return: 写入数据库的image路由地址
    """
    # 返回路径
    imagepath = None
    # 返回坐标
    # 创建指定格式excel
    import math
    from random import randint
    from openpyxl import Workbook
    from openpyxl.styles import PatternFill, Border, Side
    # 十六进制颜色取值表
    hexcolor = [chr(i) for i in range(48, 58)] + [chr(i) for i in range(65, 71)]
    # 设置excel表格填充颜色
    if len(colors) > 1:
        # 传入颜色
        colors = colors
    else:
        # 未传入颜色,设置100种随机颜色
        colors = [''.join([hexcolor[randint(0, 15)] for i in range(6)]) for i in range(100)]
    # 初始化颜色下标
    colorIndex = 0
    colorIndexm = 0
    # 设置余量
    numbers = 0
    # image
    images = 0
    # 生效颜色
    fills = [PatternFill("solid", fgColor=color) for color in colors]
    # 设置存储文件路径
    file_path = 'test.xlsx'
    # 初始化Workbook
    workbook = Workbook()
    # 创建新工作表
    sheet = workbook.create_sheet("Sheet1", 0)
    # 循环列
    # excel 行列从1开始算
    for col in range(1, cols + 1):
        t = rows - rows % spec
        # 循环行
        # excel 行列从1开始算
        for row in range(1, 1 + t):

            # 选中行列对象
            cell = sheet.cell(column=col, row=row)
            # 填充对象颜色
            cell.fill = fills[colorIndex]
            # 设置对象边框
            cell.border = Border(left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'),
                                 bottom=Side(style='thin'))
            images += 1

            print(images, math.ceil(spec / 2))
            # 设置颜色下标-满足条件切换颜色
            if row % spec == 0:
                colorIndex = (colorIndex + 1) % len(colors)
            if images == math.ceil(spec / 2):
                # 导入图片模块
                from openpyxl.drawing.image import Image
                # 读取图片
                img = Image('power.png')
                # 添加图片
                cell.add_image(img)
                images = 0

        else:
            colorIndex = (colorIndex + 1) % len(colors)
        # 剩余部分填充
        if rows % spec > 0:
            for row in range(t + 1, rows + 1):
                # 选中行列对象
                cell = sheet.cell(column=col, row=row)
                # 填充对象颜色
                cell.fill = fills[colorIndexm]
                # 设置对象边框
                cell.border = Border(left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'),
                                     bottom=Side(style='thin'))
                # 计数+1
                numbers += 1
                images += 1
                print(images, math.ceil(spec / 2))
                # 设置颜色下标-满足条件切换颜色
                if numbers % spec == 0:
                    colorIndexm = (colorIndexm + 1) % len(colors)

                if images == math.ceil(spec / 2):
                    # 导入图片模块
                    from openpyxl.drawing.image import Image
                    # 读取图片
                    img = Image('power.png')
                    # 添加图片
                    cell.add_image(img)
                    images = 0

    # 存储excel
    workbook.save(file_path)
  • 写回答

1条回答 默认 最新

  • 辉煌仪奇 2021-11-05 15:44
    关注

    我来结题了,这么久都没有人回答,其实是我自己傻了,拿到了cell对象后都不知道将其填入图片,插入图片要导入
    openpyxl的Image模块操作如下

    from openpyxl.drawing.image import Image
     # 读取图片
    img = Image('power.png')
    # 设置图片宽
    img.width = 60
    # 设置图片高
    img.height = 20
    # 添加图片
    sheet.add_image(img, cell.column_letter + str(cell.row))
    

    说明
    cell.column_letter返回的是单元格所在列如在A列返回字符串A
    cell.row返回单元格所在行,类型整形
    sheet.add_image函数的作用是将图片插入到表内指定单元格中,第一位参数为图片数据,第二位为指定单元格地址 如A5,字符串形数据
    图片插入位置为单元格的左上角
    做一下算法过滤后是这样

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月13日
  • 已采纳回答 11月5日
  • 创建了问题 11月5日

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退