xlwings 格式

xlwings 单元格格式设置

import xlwings as xw

wb = xw.Book('1.xlsx')

ws = xw.sheets[0]

ws.range(1,1).expand('right').value=[1,2,3,4]

ws.range(2,4).expand('down').options(transpose=True).value = [10,20,30,40]

wb.save()

wb.close()

app = xw.App(add_book = False)
wb = app.books.open('1.xlsx')
sheet = wb.sheets[0]

sheetFont_name = sheet.range('B1').api.Font.Name

sheetFont_size = sheet.range('B1').api.Font.Size

sheetFont_bold = sheet.range('B1').api.Font.Bold

sheetFont_color = sheet.range('B1').api.Font.Color

sheetFont_FontStyle = sheet.range('B1').api.Font.FontStyle

print('字体名称:', sheetFont_name)
print('字体大小:', sheetFont_size)
print('字体是否加粗:', sheetFont_bold)
print('字体颜色:', sheetFont_color)
print('字体类型:',sheetFont_FontStyle)

sheet.range('B1').api.Font.Name = '微软雅黑'
sheet.range('B1').api.Font.Size = 20
sheet.range('B1').api.Font.Bold = True
sheet.range('B1').api.Font.Color = 0x0000ff
sheet.range('B1').api.Font.FontStyle = "Bold Italic"

sheet.range('B1').api.HorizontalAlignment = -4108    # -4108 水平居中
sheet.range('B1').api.VerticalAlignment = -4130      # -4108 垂直居中
sheet.range('B1').api.NumberFormat = "0.00"          # 设置单元格的数字格式 

# 从范围中每个单元格的左上角到右下角的边框
sheet.range('C1').api.Borders(5).LineStyle = 1
sheet.range('C1').api.Borders(5).Weight = 2

# 从范围中每个单元格的左下角到右上角的边框
sheet.range('D1').api.Borders(6).LineStyle = 1
sheet.range('D1').api.Borders(6).Weight = 2

# 左边框,虚线
sheet.range('C3').api.Borders(7).LineStyle = 2
sheet.range('C3').api.Borders(7).Weight = 2

# 顶边框,双点划线
sheet.range('C3').api.Borders(8).LineStyle = 5
sheet.range('C3').api.Borders(8).Weight = 2

# 底部边框,直线,设置边框粗细为2
sheet.range('C3').api.Borders(9).LineStyle = 1
sheet.range('C3').api.Borders(9).Weight = 2

# 右边框,点划线
sheet.range('C3').api.Borders(10).LineStyle = 4
sheet.range('C3').api.Borders(10).Weight = 2 

 

posted @ 2021-05-04 13:57  正在学Python  阅读(1072)  评论(0编辑  收藏  举报