python excel 颜色填充 excel样式

开发一个项目的时候遇到一个问题,项目特别急,也没时间查资料,于是提了个问题,在这里插入图片描述
其实问题的描述还是不够清楚,是创建一个excel,设置m行n列的单元格为实线,每一列单元格分割s个填充颜色, 不够的像下一个单元格取(前提是所有单元格列都已经都已经填充了m//s次)
感谢 广大菜鸟给出的回答,由于我之前给的问题后面改了,但是基础功能还是有了
另外 他也出了一个博客,也是关于excel填充的python绘制表格不同颜色的excel


from openpyxl import Workbook
from openpyxl.styles import PatternFill, Side, Border
# 仿照excel格式
# excel文件路径
file_path = 'C:/Users/Lenovo/Desktop/工作簿2.xlsx'
colors = ['000000', '44546A']
fills = [PatternFill("solid", fgColor=color) for color in colors]
workbook = Workbook()
sheet = workbook.create_sheet("Sheet1", 0)0
rows, cols = 30, 9
colorIndex = 1
block_height, block_width = 5, 1
for i in range(int(rows / block_height)):
   for j in range(int(cols / block_width)):
       colorIndex = (colorIndex + 1) % 2
       for p in range(block_height):
           row = block_height * i + p
           for q in range(block_width):
               col = j * block_width + q
               cell = sheet.cell(column=col + 1, row=row + 1)
               cell.fill = fills[colorIndex]
               cell.border = Border(left=Side(style='thin'),
                                    right=Side(style='thin'),
                                    top=Side(style='thin'),
                                    bottom=Side(style='thin'))
workbook.save(file_path)

于是我根据他选中单元格,的关键函数整理重新复写了一个函数
关键函数:

cell = sheet.cell(column=col + 1, row=row + 1)
cell.fill = fills[colorIndex]
cell.border = Border(left=Side(style='thin'),right=Side(style='thin'),top=Side(style='thin'),bottom=Side(style='thin'))

整理函数如下


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

    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
    # 生效颜色
    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'))
            # 设置颜色下标-满足条件切换颜色
            if row % spec == 0:
                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
                # 设置颜色下标-满足条件切换颜色
                if numbers % spec == 0:
                    colorIndexm = (colorIndexm + 1) % len(colors)

    # 存储excel
    workbook.save(file_path)
    # excel创建完成

    # 读取excel内容转换为图片
    from PIL import ImageGrab
    import xlwings as xw
    # 使用xlwings的app启动
    app = xw.App(visible=True, add_book=False)
    # 打开文件
    wb = app.books.open(file_path)
    # 选定sheet
    sheet = wb.sheets("Sheet1")
    # 获取有内容的区域
    all = sheet.used_range
    # 复制图片区域
    all.api.CopyPicture()
    # 粘贴
    sheet.api.Paste()
    # 设置文件名
    img_name = 'data' + ".png"
    # 当前图片
    pic = sheet.pictures[0]
    # 复制图片
    pic.api.Copy()
    # 获取剪贴板的图片数据
    img = ImageGrab.grabclipboard()
    # 保存图片
    img.save(img_name)
    # 删除sheet上的图片
    pic.delete()
    # 不保存,直接关闭
    wb.close()
    # 退出xlwings的app启动
    app.quit()
    #存入数据库
    ......

    # 图片转换存入数据库完成
    imagepath = img_name
    return imagepath

函数调用

createtabletoimg(18, 9, 5)

函数效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辉煌仪奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值