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
