How To Set Cell Alignment Using xlwings?

Method

#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’

Sample Code

#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()
Set Cell Alignment Using xlwings
February 7, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *