将格式应用于整行 Openpyxl
Applying Format to Entire Row Openpyxl
我有一个要格式化的 Excel 文件。第一行(不包括 Headers 所以第 2 行)应该是红色的并且 斜体 .
Openpyxl Documentation states:
If you want to apply styles to entire rows and columns then you must apply the style to each cell yourself
我个人认为这很糟糕...这是我的解决方法:
import openpyxl
from openpyxl.styles import NamedStyle
from openpyxl import load_workbook
from openpyxl.styles.colors import RED
from openpyxl.styles import Font
# I normally import a lot of stuff... I'll also take suggestions here.
file = 'MY_PATH'
wb = load_workbook(filename=file)
sheet = wb.get_sheet_by_name('Output')
for row in sheet.iter_rows():
for cell in row:
if '2' in cell.coordinate:
# using str() on cell.coordinate to use it in sheet['Cell_here']
sheet[str(cell.coordinate)].font = Font(color='00FF0000', italic=True)
wb.save(filename=file)
第一个缺点是,如果有更多的单元格,例如 A24
,我的循环将对其应用格式。我可以用正则表达式解决这个问题。这是正确的做法吗?
最终 - 是否有更好的方法将格式应用于整行? 另外。任何人都可以为我指出一些 good Openpyxl 文档的正确方向吗?我只在 Stack 上发现了 sheet.iter_rows()
和 cell.coordinates
。
如果您只想更改第二行的颜色,则无需遍历所有行,您可以按如下方式遍历一行:
import openpyxl
from openpyxl import load_workbook
from openpyxl.styles import Font
file = 'input.xlsx'
wb = load_workbook(filename=file)
ws = wb['Output']
red_font = Font(color='00FF0000', italic=True)
# Enumerate the cells in the second row
for cell in ws["2:2"]:
cell.font = red_font
wb.save(filename=file)
给你这样的东西:
openpyxl 文档中描述了访问多个单元格:Accessing many cells
格式 "2:2"
枚举单行中的单元格。如果使用 "2:3"
,这将 return 单元格一次一行,即第 2 行然后第 3 行,因此需要一个额外的循环。
或者,使用 NamedStyle
:
import openpyxl
from openpyxl import load_workbook
from openpyxl.styles import Font, NamedStyle
file = 'input.xlsx'
wb = load_workbook(filename=file)
ws = wb['Output']
# Create a NamedStyle (if not already defined)
if 'red_italic' not in wb.named_styles:
red_italic = NamedStyle(name="red_italic")
red_italic.font = Font(color='00FF0000', italic=True)
wb.add_named_style(red_italic)
# Enumerate the cells in the second row
for cell in ws["2:2"]:
cell.style = 'red_italic'
wb.save(filename=file)
我有一个要格式化的 Excel 文件。第一行(不包括 Headers 所以第 2 行)应该是红色的并且 斜体 .
Openpyxl Documentation states:
If you want to apply styles to entire rows and columns then you must apply the style to each cell yourself
我个人认为这很糟糕...这是我的解决方法:
import openpyxl
from openpyxl.styles import NamedStyle
from openpyxl import load_workbook
from openpyxl.styles.colors import RED
from openpyxl.styles import Font
# I normally import a lot of stuff... I'll also take suggestions here.
file = 'MY_PATH'
wb = load_workbook(filename=file)
sheet = wb.get_sheet_by_name('Output')
for row in sheet.iter_rows():
for cell in row:
if '2' in cell.coordinate:
# using str() on cell.coordinate to use it in sheet['Cell_here']
sheet[str(cell.coordinate)].font = Font(color='00FF0000', italic=True)
wb.save(filename=file)
第一个缺点是,如果有更多的单元格,例如 A24
,我的循环将对其应用格式。我可以用正则表达式解决这个问题。这是正确的做法吗?
最终 - 是否有更好的方法将格式应用于整行? 另外。任何人都可以为我指出一些 good Openpyxl 文档的正确方向吗?我只在 Stack 上发现了 sheet.iter_rows()
和 cell.coordinates
。
如果您只想更改第二行的颜色,则无需遍历所有行,您可以按如下方式遍历一行:
import openpyxl
from openpyxl import load_workbook
from openpyxl.styles import Font
file = 'input.xlsx'
wb = load_workbook(filename=file)
ws = wb['Output']
red_font = Font(color='00FF0000', italic=True)
# Enumerate the cells in the second row
for cell in ws["2:2"]:
cell.font = red_font
wb.save(filename=file)
给你这样的东西:
openpyxl 文档中描述了访问多个单元格:Accessing many cells
格式 "2:2"
枚举单行中的单元格。如果使用 "2:3"
,这将 return 单元格一次一行,即第 2 行然后第 3 行,因此需要一个额外的循环。
或者,使用 NamedStyle
:
import openpyxl
from openpyxl import load_workbook
from openpyxl.styles import Font, NamedStyle
file = 'input.xlsx'
wb = load_workbook(filename=file)
ws = wb['Output']
# Create a NamedStyle (if not already defined)
if 'red_italic' not in wb.named_styles:
red_italic = NamedStyle(name="red_italic")
red_italic.font = Font(color='00FF0000', italic=True)
wb.add_named_style(red_italic)
# Enumerate the cells in the second row
for cell in ws["2:2"]:
cell.style = 'red_italic'
wb.save(filename=file)