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