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


免責聲明!

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



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