在Openpyxl中设置样式

在Openpyxl中设置样式,第1张

在Openpyxl中设置样式

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

from openpyxl.reader.excel import load_workbookfrom openpyxl.workbook import Workbookfrom openpyxl.styles import Color, Fillfrom 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
…中找到颜色名称。有时我会从X11颜色名称中添加其他颜色

class Color(HashableObject):    """Named colors for use in styles."""    BLACK = 'FF000000'    WHITE = 'FFFFFFFF'    RED = 'FFFF0000'    DARKRED = 'FF800000'    BLUE = 'FF0000FF'    DARKBLUE = 'FF000080'    GREEN = 'FF00FF00'    DARKGREEN = 'FF008000'    YELLOW = 'FFFFFF00'    DARKYELLOW = 'FF808000'


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5644503.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存