我想应用特定的格式或为Excel单元格范围分配一个值,而无需遍历每个单元格.我目前正在使用此脚本:
from openpyxl import Workbook
from openpyxl.styles import Font
wb = Workbook()
ws = wb.active
## With iterations
# Apply style
for i, rowOfCellObjects in enumerate(ws['A1':'C4']):
for n, cellObj in enumerate(rowOfCellObjects):
cellObj.fill = Font(name='Times New Roman')
# Assign singular value to all cells
for i, rowOfCellObjects in enumerate(ws['A1':'C4']):
for n, cellObj in enumerate(rowOfCellObjects):
cellObj.value = 3
wb.save("test.xlsx")
但是我正在寻找一个较短的符号,像这样:
from openpyxl import Workbook
from openpyxl.styles import Font
wb = Workbook()
ws = wb.active
## Without iterations
# Apply style
ws['A1':'C4'].fill = Font(name='Times New Roman')
# Assign singular value to all cells
ws['A1':'C4'].value = 3
wb.save("test.xlsx")
openpyxl或其他模块是否提供类似的信息?
ps:我正在使用Python 3.5
解决方法:
在Excel中,必须将样式应用于单个单元格,因为这是文件格式的工作方式.由于它的工作方式,openpyxl无法提供所需的功能,但是xlsxwriter可能是可行的.