我想指定表格中各列的样式。如果我只选择一列作为风格,它的工作,但如果我想指定一个以上的列。我得到了这个错误信息。属性错误:'元组'对象没有属性'样式'。
import openpyxl
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.styles import Font, Color, Alignment, Border, Side, colors
from openpyxl.styles import NamedStyle
path = "my xlsx file path"
workbook = load_workbook(path)
sheet = workbook.active
sheet
shetnames_str = workbook.sheetnames
print(shetnames_str)
print(sheet)
dimensions = sheet.dimensions
print("Dimenzió: "+dimensions)
selected_sheetname = workbook["Munka1"]
#Style definition
highlight = NamedStyle(name="highlight")
bd = Side(style='thin', color="000000")
highlight.border = Border(left=bd, top=bd, right=bd, bottom=bd)
for cell in sheet["A:B"]:
print(cell)
cell.style = highlight
workbook.save(filename="moddedxlsx.xlsx")
我修改了我的代码
import openpyxl
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.styles import Font, Color, Alignment, Border, Side, colors
from openpyxl.styles import NamedStyle
path = "C:\\Users\\Jutasig\\Documents\\python_jegyzetek\\Dev\\Style\\master_xls_style\\szamla.xlsx"
workbook = load_workbook(path)
sheet = workbook.active
sheet
shetnames_str = workbook.sheetnames
print(shetnames_str)
print(sheet)
dimensions = "\""+sheet.dimensions+"\""
print("Dimenzió: "+dimensions)
selected_sheetname = workbook["Munka1"]
#Style definition
def set_border(ws, cell_range):
border = Border(left=Side(border_style='thin', color='000000'),
right=Side(border_style='thin', color='000000'),
top=Side(border_style='thin', color='000000'),
bottom=Side(border_style='thin', color='000000'))
rows = ws[cell_range]
for row in rows:
for cell in row:
cell.border = border
set_border(sheet, sheet.dimensions)
workbook.save(filename="moddedxlsx.xlsx")