python openpyxl表格樣式設置


為美化openpyxl表格,特設置樣式,把有關參數與大家分享

from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.worksheet.table import Table, TableStyleInfo
 
wb = Workbook()
ws = wb.active
 
data = [
    ['Apples', 10000, 5000, 8000, 6000],
    ['Pears',   2000, 3000, 4000, 5000],
    ['Bananas', 6000, 6000, 6500, 6000],
    ['Oranges',  500,  300,  200,  700],
]
 
ws.append(["Fruit", "2011", "2012", "2013", "2014"])
for row in data:
    ws.append(row)
 
tab = Table(displayName="Table1", ref="A1:E5")  #名稱管理器 如E超出數據范圍出錯:warn("File may not be readable: column headings must be strings.")
 
 # 'TableStyleLight11' 1-21 還有此樣式  "TableStyleMedium9" 1-28  TableStyleDark1  1-11
 #  showFirstColumn=True,
 #  showLastColumn=True, showRowStripes=True, showColumnStripes=True)
 # 第一行是否和樣式第一行顏色一樣,第二列是否···
 # 是否隔行換色,是否隔列換色
style = TableStyleInfo(name='TableStyleDark11', showFirstColumn=True,
                       showLastColumn=True, showRowStripes=False, showColumnStripes=True)
tab.tableStyleInfo = style
ws.add_table(tab)
wb.save('d:\\s2.xlsx')

ws.append(["Fruit", "2011", "2012", "2013", "2014"])
for row in data:
    ws.append(row)
 
tab = Table(displayName="Table2", ref="A6:E10")  #名稱管理器
 
 # 'TableStyleLight11' 1-21 還有此樣式  "TableStyleMedium9" 1-28  TableStyleDark1  1-11
 #  showFirstColumn=True,
 #  showLastColumn=True, showRowStripes=True, showColumnStripes=True)
 # 第一行是否和樣式第一行顏色一樣,第二列是否···
 # 是否隔行換色,是否隔列換色
style = TableStyleInfo(name='TableStyleDark11', showFirstColumn=False,
                       showLastColumn=True, showRowStripes=True, showColumnStripes=True)
tab.tableStyleInfo = style
ws.add_table(tab)
wb.save('d:\\s2.xlsx')

ws.append(["Fruit", "2011", "2012", "2013", "2014"])
for row in data:
    ws.append(row)
 
tab = Table(displayName="Table3", ref="A11:E15")  #名稱管理器
 
 # 'TableStyleLight11' 1-21 還有此樣式  "TableStyleMedium9" 1-28  TableStyleDark1  1-11
 #  showFirstColumn=True,
 #  showLastColumn=True, showRowStripes=True, showColumnStripes=True)
 # 第一行是否和樣式第一行顏色一樣,第二列是否···
 # 是否隔行換色,是否隔列換色
style = TableStyleInfo(name='TableStyleDark11', showFirstColumn=False,
                       showLastColumn=True, showRowStripes=True, showColumnStripes=False)
tab.tableStyleInfo = style
ws.add_table(tab)
wb.save('d:\\s2.xlsx')
python openpyxl表格樣式設置_第1張圖片
運行結果,后面是參數,T代表True,F代表Flase,分別代表與前行、列的同樣樣式,跳行列是否變樣式
三種大類樣式
‘TableStyleLight11’ 1-21 還有此樣式 “TableStyleMedium9” 1-28 TableStyleDark1 1-11

python openpyxl表格樣式設置_第2張圖片

分別代表淺中深三大種類,后面是編號

原文鏈接 

https://www.it610.com/article/1282210922240163840.htm

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM