Openpyxl:格式化整行或整列直到指定单元格 – 是否可行?

huangapple go评论68阅读模式
英文:

Openpyxl: Formatting an entire row or column up to a designated cell - is it doable?

问题

在openpyxl中,是否有一种方法可以格式化整个行或列,并使其在特定单元格处停止,而不必遍历每个单独的单元格。

例如,我是否可以执行类似于调用ws.row_dimensions[0].font = some_font_object的操作,以便它格式化整个行[0],但在列F处停止。

我有一个用于同时格式化整个行和列的函数,通过遍历每个单独的单元格来实现。只是想知道是否可以对第一种选项设置一个限制。

英文:

In openpyxl, is there a way to format an entire row or column and have it stop at a certain cell without iterating over each individual cell.

I.e. can I do something similar to calling ws.row_dimensions[0].font = some_font_object, so that it formats the font of the entire row[0], but have it stop at say column F.

I have a function for formatting both entire rows and columns at once and by iterating over each individual cell. Was just wondering if I can set a limit to the first option.

答案1

得分: 1

以下是翻译好的部分:

"Is there a way to format an entire row or column and have it stop at a certain cell without iterating over each individual cell?"
有没有一种方法可以格式化整行或整列,并使其在特定单元格停止,而无需遍历每个单元格?

"I'm not sure if this would be possible in openpyxl (read this answer from @Charlie Clark)."
我不确定在openpyxl中是否可能实现这一点(阅读@Charlie Clark此答案)。

"AFAIK, you need to loop over the cells individually, this way :"
据我所知,你需要逐个遍历单元格,像这样:

"from openpyxl import Workbook
from openpyxl.styles import NamedStyle, Font

wb = Workbook()
ws = wb.active

randomStyle = NamedStyle(
name="randomStyle",
font=Font(name="Arial", size=40, bold=True, color="33c481"),
# (if needed) add here more styles
)

ws.append(range(10)) # here we write values in A0:J0

for cell in ws["A0:F0"][0]: # here, we format a specific range
cell.style = randomStyle

#another variant
#for row in ws.iter_rows(max_row=1, max_col=6):

for cell in row:

cell.style = randomStyle

wb.save("file.xlsx")"

输出:

Openpyxl:格式化整行或整列直到指定单元格 – 是否可行?

英文:

> Is there a way to format an entire row or column and have it stop at a
> certain cell without iterating over each individual cell ?

I'm not sure if this would be possible in openpyxl (read this answer from @Charlie Clark).

AFAIK, you need to loop over the cells individually, this way :

from openpyxl import Workbook
from openpyxl.styles import NamedStyle, Font

wb = Workbook()
ws = wb.active

randomStyle = NamedStyle(
    name="randomStyle",
    font=Font(name="Arial", size=40, bold=True, color="33c481"),
    # (if needed) add here more styles
)
    
ws.append(range(10)) # here we write values in A0:J0

for cell in ws["A0:F0"][0]: # here, we format a specific range
    cell.style = randomStyle

#another variant
#for row in ws.iter_rows(max_row=1, max_col=6):
#    for cell in row:
#        cell.style = randomStyle
    
wb.save("file.xlsx")

Output :

Openpyxl:格式化整行或整列直到指定单元格 – 是否可行?

huangapple
  • 本文由 发表于 2023年6月1日 23:05:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定