Python 在Openpyxl中设置样式

Python 在Openpyxl中设置样式,python,excel,xlsx,openpyxl,Python,Excel,Xlsx,Openpyxl,我需要在Openpyxl中设置样式的建议 我知道可以设置单元格的NumberFormat,但我还需要设置字体颜色和属性(粗体等)。有一个style.py类,但我似乎无法设置单元格的style属性,我也不想开始修改openpyxl源代码 有人找到了解决方案吗?从openpyxl 1.5.7版开始,我已成功应用了以下工作表样式选项 from openpyxl.reader.excel import load_workbook from openpyxl.workbook import Workboo

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

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


有人找到了解决方案吗?

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

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
仅供参考,您可以在
openpyxl/style.py
中找到颜色的名称。。。我有时会从中添加额外的颜色


从openpyxl-1.7.0开始,您也可以这样做:

cell.style.fill.start_color.index = "FF124191"

我有两个助手函数,可以在给定的
单元格上设置样式,例如页眉、页脚等。

从openpyxl 2.0开始,样式是不可变的

如果您有一个
单元格
,您可以(例如)通过以下方式设置粗体文本:

cell.style=cell.style.copy(font=cell.style.font.copy(bold=True))


是的,这很烦人。

从openpyxl 2.0开始,设置单元格样式是通过创建新的样式对象并将它们指定给单元格的属性来完成的

有几个样式对象:
Font
PatternFill
Border
Alignment
。看

要更改单元格的样式特性,首先必须从单元格复制现有样式对象并更改特性值,或者必须使用所需设置创建新样式对象。然后,将新样式对象指定给单元

将单元格A1的字体设置为粗体和斜体的示例:

from openpyxl import Workbook
from openpyxl.styles import Font
# Create workbook
wb = Workbook()
# Select active sheet
ws = wb.active()
# Select cell A1
cell = ws['A1']
# Make the text of the cell bold and italic
cell.font = cell.font.copy(bold=True, italic=True)
就像我说的:

这是一个开源项目,由志愿者在业余时间维护。这很可能意味着您想要的特定特性或功能缺失

我检查了openpyxl源代码,发现:

在openpyxl 1.8.x之前,样式是可变的。可以像这样直接指定其属性:

from openpyxl.workbook import Workbook
from openpyxl.style import Color

wb = Workbook()
ws = wb.active
ws['A1'].style.font.color.index = Color.RED
然而,从OpenPYXL1.9开始,样式是不可变的

样式在对象之间共享,一旦指定样式,就无法更改。这样可以阻止不必要的副作用,例如在更改大量单元格的样式时,而不是仅更改一个单元格的样式

要创建新的样式对象,您可以直接指定它,或者使用新属性从现有单元格的样式复制一个样式对象,请回答以下问题作为示例(请原谅我的中英文):

单元格样式包含以下属性:字体、填充、边框、对齐、保护和数字格式。选中
openpyxl.styles

它们类似,应该作为对象创建,除了数字格式,其值为
string
type

一些预定义的数字格式可用,数字格式也可以以字符串类型定义。检查
openpyxl.style.number

from openpyxl.styles import numbers

# use pre-defined values
ws.cell['T49'].number_format = numbers.FORMAT_GENERAL
ws.cell(row=2, column=4).number_format = numbers.FORMAT_DATE_XLSX15

# use strings
ws.cell['T57'].number_format = 'General'
ws.cell(row=3, column=5).number_format = 'd-mmm-yy'
ws.cell['E5'].number_format = '0.00'
ws.cell['E50'].number_format = '0.00%'
ws.cell['E100'].number_format = '_ * #,##0_ ;_ * -#,##0_ ;_ * "-"??_ ;_ @_ '
对于openpyxl 2.4.1及以上版本,请使用以下代码设置字体颜色: 各种颜色的十六进制代码可在以下位置找到:

这似乎是一个已经改变了几次的功能。我使用的是openpyxl 2.5.0,我可以通过以下方式设置strike through选项:

new_font = copy(cell.font)
new_font.strike = True
cell.font = new_font
早期版本(1.9到2.4?)的字体上似乎有一个
copy
方法,该方法现在已被弃用,并发出警告:

cell.font = cell.font.copy(strike=True)
高达1.8的版本具有可变字体,因此您可以执行以下操作:

cell.font.strike=True
现在出现了一个错误。

New 2021更新了OpenPyXl中更改字体的方法: 完整代码:

import openpyxl  # Connect the library
from openpyxl import Workbook
from openpyxl.styles import PatternFill  # Connect cell styles
from openpyxl.workbook import Workbook
from openpyxl.styles import Font, Fill  # Connect styles for text
from openpyxl.styles import colors  # Connect colors for text and cells

wb = openpyxl.Workbook()  # Create book
work_sheet = wb.create_sheet(title='Testsheet')  # Created a sheet with a name and made it active

work_sheet['A1'] = 'Test text'
work_sheet_a1 = work_sheet['A5']  # Created a variable that contains cell A1 with the existing text
work_sheet_a1.font = Font(size=23, underline='single', color='FFBB00', bold=True,
                          italic=True)  # We apply the following parameters to the text: size - 23, underline, color = FFBB00 (text color is specified in RGB), bold, oblique. If we do not need a bold font, we use the construction: bold = False. We act similarly if we do not need an oblique font: italic = False.

# Important:
# if necessary, the possibility of using standard colors is included in the styles, but the code in this case will look different:
work_sheet_a1.font = Font(size=23, underline='single', color=colors.RED, bold=True,
                              italic=True)  # what color = colors.RED — color prescribed in styles
work_sheet_a1.fill = PatternFill(fill_type='solid', start_color='ff8327',
                                 end_color='ff8327')  # This code allows you to do design color cells

现在您可以将字体颜色添加为:_cell.style.font.color.index=color.green无需修补openpyxl/style.py。只需在程序中定义类似“Color.Aquamarine='007FFFD4'”的内容,并与…=Color.aquamarine这意味着它应该定义一次,并链接多次。:)我尝试了此操作,但出现了以下错误:AttributeError:“Font”对象没有属性“copy”如何为字符串中的特定单词设置粗体?“cell.Font=cell.Font.copy(strike=True)”有效。ThanksHow我应该为字符串中的特定单词设置strike吗?我不确定,@VineeshTP。我建议你为此提出一个新问题。您可以尝试在电子表格软件中设置所需的格式,然后使用OpenPyxl阅读它,查看它在其中的表示方式。请查找问题,
cell.font = cell.font.copy(strike=True)
cell.font.strike=True
sheet.cell.font = Font(size=23, underline='single', color='FFBB00', bold=True, italic=True)
import openpyxl  # Connect the library
from openpyxl import Workbook
from openpyxl.styles import PatternFill  # Connect cell styles
from openpyxl.workbook import Workbook
from openpyxl.styles import Font, Fill  # Connect styles for text
from openpyxl.styles import colors  # Connect colors for text and cells

wb = openpyxl.Workbook()  # Create book
work_sheet = wb.create_sheet(title='Testsheet')  # Created a sheet with a name and made it active

work_sheet['A1'] = 'Test text'
work_sheet_a1 = work_sheet['A5']  # Created a variable that contains cell A1 with the existing text
work_sheet_a1.font = Font(size=23, underline='single', color='FFBB00', bold=True,
                          italic=True)  # We apply the following parameters to the text: size - 23, underline, color = FFBB00 (text color is specified in RGB), bold, oblique. If we do not need a bold font, we use the construction: bold = False. We act similarly if we do not need an oblique font: italic = False.

# Important:
# if necessary, the possibility of using standard colors is included in the styles, but the code in this case will look different:
work_sheet_a1.font = Font(size=23, underline='single', color=colors.RED, bold=True,
                              italic=True)  # what color = colors.RED — color prescribed in styles
work_sheet_a1.fill = PatternFill(fill_type='solid', start_color='ff8327',
                                 end_color='ff8327')  # This code allows you to do design color cells