zoukankan      html  css  js  c++  java
  • [TimLinux] openpyxl 操作Excel

    from openpyxl import Workbook
    from openpyxl.styles import Color, PatternFill, Font
    from openpyxl.styles.fills import FILL_SOLID
    from openpyxl.styles.colors import BLACK
    from openpyxl.utils import get_column_letter
    
    
    wb = Workbook()
    ws = wb.active
    c = ws['A1']
    
    fill = PatternFill(patternType=FILL_SOLID, fgColor=(rgb="0000FF"))  # 这里使用的是 RGB格式
    font = Font(color="FFBB00")  # 这里使用RGB格式
    ws.sheet_properties.tabColor = BLACK
    
    # 填充一个单元格
    c.value = "this is new value"
    c.font = font
    c.fill = fill
    
    # 填充一行
    cells = ws.row_dimensions[1]
    row.font = font
    row.fill = fill
    
    # 填充指定长度的行
    start_chr = "A"
    end_chr = get_column_letter(5)  # 这个方法可以解决AA列名问题
    fill_cells = ws["%s1" % start_chr  :  "%s1" % end_chr]
    for tmp_c in fill_cells[0]:
        tmp_c.font = font
        tmp_c.fill = font
    
    wb.save(filename='test.xlsx')
     
  • 相关阅读:
    迪杰斯特拉(Dijkstra)算法描述及理解
    KMP初步
    网络流初步
    Cutting Codeforces Round #493 (Div. 2)
    优先队列小结
    树状数组初步理解
    分块思想
    树状数组-逆序对-HDU6318
    线段树
    8.12.5
  • 原文地址:https://www.cnblogs.com/timlinux/p/9950613.html
Copyright © 2011-2022 走看看