天天看點

python操作excel之設定字型顔色及格式

xlwings如何設定字型、顔色等屬性

# coding: utf-8

import xlwings as xw

app=xw.App(visible=False,add_book=False)
filepath = \'../data/test.xlsx\'
wb=app.books.open(filepath)
sht = wb.sheets(\'Sheet1\')
font_name = sht.range(\'A1\').api.Font.Name    # 擷取字型名稱
font_size = sht.range(\'A1\').api.Font.Size    # 擷取字型大小
bold = sht.range(\'A1\').api.Font.Bold    # 擷取是否加粗,True--加粗,False--未加粗
color = sht.range(\'A1\').api.Font.Color    # 擷取字型顔色
print(font_name)
print(font_size)
print(bold)
print(color)
wb.save()
wb.close()
app.quit()      
# coding: utf-8

import xlwings as xw

app=xw.App(visible=False,add_book=False)
filepath = \'../data/test.xlsx\'
wb=app.books.open(filepath)
sht = wb.sheets(\'Sheet1\')
font_name = sht.range(\'A1\').api.Font.Name    # 擷取字型名稱
font_size = sht.range(\'A1\').api.Font.Size    # 擷取字号
bold = sht.range(\'A1\').api.Font.Bold    # 擷取是否加粗,True--加粗,False--未加粗
color = sht.range(\'A1\').api.Font.Color    # 擷取字型顔色
print(font_name)
print(font_size)
print(bold)
print(color)
print(\'-----設定-----\')
sht.range(\'A1\').api.Font.Name = \'Times New Roman\'    # 設定字型為Times New Roman
sht.range(\'A1\').api.Font.Size = 15    # 設定字号為15
sht.range(\'A1\').api.Font.Bold = True    # 加粗
sht.range(\'A1\').api.Font.Color = 0x0000ff    # 設定為紅色RGB(255,0,0)
font_name = sht.range(\'A1\').api.Font.Name    # 擷取字型名稱
font_size = sht.range(\'A1\').api.Font.Size    # 擷取字型大小
bold = sht.range(\'A1\').api.Font.Bold    # 擷取是否加粗,True--加粗,False--未加粗
color = sht.range(\'A1\').api.Font.Color    # 擷取字型顔色

print(font_name)
print(font_size)
print(bold)
print(color)
wb.save()
wb.close()
app.quit()      

設定字型格式

sht.range(\'A1\').api.Font.Name=‘微軟雅黑’

參考資料

[1] @牧靈雲

原文連結:https://blog.csdn.net/weixin_37577134/java/article/details/89048798