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