Skip to content

Instantly share code, notes, and snippets.

@pythonexcels
Created September 19, 2012 07:37
Show Gist options
  • Save pythonexcels/3748214 to your computer and use it in GitHub Desktop.
Save pythonexcels/3748214 to your computer and use it in GitHub Desktop.
Set Excel spreadsheet row height with Python
#
# Set row heights and align text within the cell
#
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()
ws = wb.Worksheets("Sheet1")
ws.Range("A1:A2").Value = "1 line"
ws.Range("B1:B2").Value = "Two\nlines"
ws.Range("C1:C2").Value = "Three\nlines\nhere"
ws.Range("D1:D2").Value = "This\nis\nfour\nlines"
ws.Rows(1).RowHeight = 60
ws.Range("2:2").RowHeight = 120
ws.Rows(1).VerticalAlignment = win32.constants.xlCenter
ws.Range("2:2").VerticalAlignment = win32.constants.xlCenter
# Alternately, you can autofit all rows in the worksheet
# ws.Rows.AutoFit()
wb.SaveAs('row_height.xlsx')
excel.Application.Quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment