`Index` can be one of the following `XlBordersIndex` constants: xlDiagonalDown、xlDiagonalUp、xlEdgeBottom、xlEdgeLeft、xlEdgeRight、xlEdgeTop、xlInsideHorizontal or xlInsideVertical。
#Cell comments
import xlwings as xw #Import the xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
#xlwings API
sht.api.Range('B2').CurrentRegion.Borders.LineStyle=xw.constants.LineStyle.xlContinuous
sht.api.Range('B2').CurrentRegion.Borders.ColorIndex=3
sht.api.Range('B2').CurrentRegion.Borders.Weight=xw.constants.BorderWeight.xlThick
#bk.close()
#app.kill()
#Background color of the cell
import xlwings as xw #Import the xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
#xlwings
#sht.range('A1:E1').color=(210, 67, 9)
sht['A:A, B2, C5, D7:E9'].color=(100,200,150)
#xlwings API
#sht.api.Range('A1:E1').Interior.Color=xw.utils.rgb_to_int((0, 255, 0))
#sht.api.Range('A1:E1').Interior.Color=65280
#sht.api.Range('A1:E1').Interior.ColorIndex=6
#sht.api.Range('A1:E1').Interior.ThemeColor=5
#bk.close()
#app.kill()
#Cell Alignment
import xlwings as xw #Import the xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
#Setting the alignments of cell C3
sht.api.Range('C3').HorizontalAlignment = xw.constants.Constants.xlCenter
sht.api.Range('C3').VerticalAlignment = xw.constants.Constants.xlCenter
sht.api.Range('C3').Value='Test1'
#bk.close()
#app.kill()
#sht.api.Range(‘A3:E3’).Font.Color =16711680 # or 0x0000FF
#sht.api.Range(‘A1:E1’).Font.ColorIndex = 3
#sht.api.Range(‘A3:E3’).Font.ThemeColor =5
sht.api.Range(‘C3′).Value=’Test456’
Sample Code
#Cell Font Styles
import xlwings as xw #Import the xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
#xlwings API
sht.api.Range('A1:E1').Font.Name = 'Arial' #Set the font to Arial
sht.api.Range('A1:E1').Font.ColorIndex = 3 #Set the font color to red
sht.api.Range('A1:E1').Font.Size = 20 #Set the font size to 20
sht.api.Range('A1:E1').Font.Bold = True #Set the font to bold
sht.api.Range('A1:E1').Font.Italic = True #Set the font to italic
sht.api.Range('A1:E1').Font.Underline=xw.constants.UnderlineStyle.xlUnderlineStyleDouble #Add a double underline to the text
sht.api.Range('C1').Value='Test123'
sht.api.Range('A3:E3').Font.Color =xw.utils.rgb_to_int((0, 0, 255))
#sht.api.Range('A3:E3').Font.Color =16711680 # 或0x0000FF
#sht.api.Range('A1:E1').Font.ColorIndex = 3
#sht.api.Range('A3:E3').Font.ThemeColor =5
sht.api.Range('C3').Value='Test456'
#bk.close()
#app.kill()
#Cell comments
import xlwings as xw #Import the xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
#xlwings API
sht.api.Range('A3').AddComment(Text='Cell comments')
#Check if cell A3 has a comment
if sht.api.Range('A3').Comment is None:
print('Cell A3 has no comment.')
else:
print('Cell A3 has a comment.')
#Hide a comment in cell A3
sht.api.Range('A3').Comment.Visible=False
#Delete a comment in cell A3
sht.api.Range('A3').Comment.Delete()
#bk.close()
#app.kill()
#Select and clear cells
import xlwings as xw #Import the xlwings package
app=xw.App()
bk=app.books.open('special.xlsx')
sht=bk.sheets.active #Get the active worksheet
#Select
#xlwings
#sht.range('A1:B10').select()
#xlwings API
#sht.api.Range('A1:B10').Select()
#sht.api.Range('A1:B10').Activate()
#Select non-contiguous cell ranges
#xlwings
#sht.range('A1:A5,C3,E1:E5').select()
#xlwings API
#sht.api.Range('A1:A5,C3,E1:E5').Activate()
#sht.api.Range('A1:A5,C3,E1:E5').Select()
#Clear all content from a cell range
#xlwings
#sht.range('B2:D5').clear()
#xlwings API
sht.api.Range('B2:D5').Clear()
#Clear text content of a cell
#xlwings
#sht.range('B2:D5').clear_contents()
#xlwings API
#sht.api.Range('B2:D5').ClearContents()
#Clear cell comments
#xlwings API
#sht.api.Range('B2:D5').ClearComments()
#Clear cell formatting
#xlwings API
#sht.api.Range('B2:D5').ClearFormats()
#bk.close()
#app.kill()
#Inserting Cells or Ranges
import xlwings as xw #Import the xlwings package
app=xw.App()
bk=app.books.open('insert.xlsx')
sht=bk.sheets.active #Get the active worksheet
#xlwings
sht.range('C3').insert(shift='down',copy_origin='format_from_left_or_above')
#sht.range('B3:C5').insert()
#xlwings API
#sht.api.Range('A2').Insert(Shift=xw.constants.InsertShiftDirection.xlShiftDown, CopyOrigin=xw.constants.InsertFormatOrigin.xlFormatFromLeftOrAbove)
#sht.api.Range('B4:C5').Insert()
#bk.close()
#app.kill()