使用openpyxl用颜色填充单元格? [英] Fill cells with colors using openpyxl?

查看:662
本文介绍了使用openpyxl用颜色填充单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将openpyxl v2.2.2用于Python 2.7,我想为单元格设置颜色.我已经使用了以下导入

I am currently using openpyxl v2.2.2 for Python 2.7 and i wanted to set colors to cells. I have used the following imports

import openpyxl,
from openpyxl import Workbook
from openpyxl.styles import Color, PatternFill, Font, Border
from openpyxl.styles import colors
from openpyxl.cell import Cell

以下是我尝试使用的代码:

and the following is the code I tried using:

wb = openpyxl.Workbook()
ws = wb.active

redFill = PatternFill(start_color='FFFF0000',
                   end_color='FFFF0000',
                   fill_type='solid')

ws['A1'].style = redFill

但出现以下错误:

Traceback (most recent call last)
  self.font = value.font.copy()
AttributeError: 'PatternFill' object has no attribute 'font'

关于如何使用openpyxl为单元格A1(或其他任何单元格)设置颜色的想法?

Any idea on how to set cell A1 (or any other cells) with colors using openpyxl?

推荐答案

我相信问题在于您正在尝试将填充对象分配给样式.

I believe the issue is that you're trying to assign a fill object to a style.

ws['A1'].fill = redFill应该可以正常工作.

这篇关于使用openpyxl用颜色填充单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆