在Openpyxl中设置样式 [英] Setting styles in Openpyxl

查看:997
本文介绍了在Openpyxl中设置样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Openpyxl中设置样式的建议。



我看到可以设置单元格的NumberFormat,但是我也需要设置字体颜色和属性(大胆等)。有一个style.py类,但是我似乎无法设置单元格的style属性,而我真的不想开始修改openpyxl源代码。



有没有人找到解决方案?

解决方案

从openpyxl版本1.5.7开始,我已经成功应用了以下工作表样式选项...

  from openpyxl.reader.excel import load_workbook 
from openpyxl.workbook import Workbook
从openpyxl.styles导入颜色,从openpyxl.cell导入填充
单元格

#加载工作簿...
book = load_workbook('foo.xlsx')

#define ws这里,在这种情况下,我选择工作簿中的第一个工作表...
#注意:openpyxl有其他方法来选择一个特定的工作表(即名称
#via book.get_sheet_by_name('someWorksheetName'))
ws = book.worksheets [0]

## ws是一个openpypxl工作表对象
_cell = ws.cell(' C1')

#字体属性
_cell.style.font.color.index = Color.GREEN
_cell.style.font.name ='Arial'
_cell.style.font.size = 8
_cell.style.font.bold = True
_cell.style.alignment.wrap_text = True

#单元格背景颜色
_cell.style.fill.fill_type = Fill.FILL_SOLID
_cell.style.fill.start_color.index = Color.DARKRED

#在
#列中写入单元格后,您应该只修改列维度。完美的世界:每列写列维度

ws.column_dimensions [C]。width = 60.0

FYI,您可以在 openpyxl / style.py 中找到颜色的名称...我有时候从<一个href =http://en.wikipedia.org/wiki/Web_colors#X11_color_names =noreferrer> X11颜色名称

  class Color(HashableObject):
用于样式的命名颜色。
BLACK ='FF000000'
WHITE ='FFFFFFFF'
RED ='FFFF0000'
DARKRED ='FF800000'
BLUE ='FF0000FF'
DARKBLUE ='FF000080'
GREEN ='FF00FF00'
DARKGREEN = 'FF008000'
YELLOW ='FFFFFF00'
DARKYELLOW ='FF808000'


I need advice on setting styles in Openpyxl.

I see that the NumberFormat of a cell can be set, but I also require setting of font colors and attributes (bold etc). There is a style.py class but it seems I can't set the style attribute of a cell, and I don't really want to start tinkering with the openpyxl source code.

Has anyone found a solution to this?

解决方案

As of openpyxl version 1.5.7, I have successfully applied the following worksheet style options...

from openpyxl.reader.excel import load_workbook
from openpyxl.workbook import Workbook
from openpyxl.styles import Color, Fill
from openpyxl.cell import Cell

# Load the workbook...
book = load_workbook('foo.xlsx')

# define ws here, in this case I pick the first worksheet in the workbook...
#    NOTE: openpyxl has other ways to select a specific worksheet (i.e. by name
#    via book.get_sheet_by_name('someWorksheetName'))
ws = book.worksheets[0]

## ws is a openpypxl worksheet object
_cell = ws.cell('C1')

# Font properties
_cell.style.font.color.index = Color.GREEN
_cell.style.font.name = 'Arial'
_cell.style.font.size = 8
_cell.style.font.bold = True
_cell.style.alignment.wrap_text = True

# Cell background color
_cell.style.fill.fill_type = Fill.FILL_SOLID
_cell.style.fill.start_color.index = Color.DARKRED

# You should only modify column dimensions after you have written a cell in 
#     the column. Perfect world: write column dimensions once per column
# 
ws.column_dimensions["C"].width = 60.0

FYI, you can find the names of the colors in openpyxl/style.py... I sometimes I patch in extra colors from the X11 color names

class Color(HashableObject):
    """Named colors for use in styles."""
    BLACK = 'FF000000'
    WHITE = 'FFFFFFFF'
    RED = 'FFFF0000'
    DARKRED = 'FF800000'
    BLUE = 'FF0000FF'
    DARKBLUE = 'FF000080'
    GREEN = 'FF00FF00'
    DARKGREEN = 'FF008000'
    YELLOW = 'FFFFFF00'
    DARKYELLOW = 'FF808000'

这篇关于在Openpyxl中设置样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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