Openpyxl:将背景色设置为行和列属性错误 [英] Openpyxl: set background color to a row and column Attribute Error

查看:184
本文介绍了Openpyxl:将背景色设置为行和列属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里查看了一些示例后,我尝试将背景色设置为整个行和列.我已经完成

having looked at a few examples over here I tried to set background color to an entire row and column. I have done

`import openpyxl
 from openpyxl.styles import PatternFill
 wb = openpyxl.load_workbook(self.inputfile)
 ws = wb.active
 ws['A1'].fill = PatternFill(bgColor="FFC7CE", fill_type = "solid")`

如果我执行ws[1].fill =PatternFill(bgColor="FFC7CE", fill_type = "solid")

上面的代码填充单个单元格(A1).但是,如果要填充整个行(1)和整个列(A),该如何前进.

The above code fills a single cell(A1). But how do I go forward if I want to fill an entire row(1), and an entire column(A).

推荐答案

从指定的min_col=1参数的列开始,迭代所有列.
在第一行之后结束,因为此处给定的参数min_row=1max_row=1相等.
参数min_row/max_row可以指向任何行,甚至可以指向外部数据.

Iterates all columns, starting at the Column given be min_col=1 argument.
Ends after one row, as here the given arguments min_row=1 and max_row=1 are equal.
Arguments min_row/max_row can point to any row, even also outside data.

  for rows in ws.iter_rows(min_row=1, max_row=1, min_col=1):
    for cell in rows:
      cell.fill = PatternFill(bgColor="FFC7CE", fill_type = "solid")

对于整列使用

iter_cols(min_col=None, max_col=None, min_row=None, max_row=None)

如果仅提供min_*属性值,则使用最大行数/列数.

If you only give min_* attribute values, max row/column are used.

使用Python:3.4.2-openpyxl:2.4.1

Tested with Python:3.4.2 - openpyxl:2.4.1

这篇关于Openpyxl:将背景色设置为行和列属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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