Question

I want the text in an Excel cell to align with the top, rather then with the bottom, when writing a file from python using openpyxl

Was it helpful?

Solution 2

Try this:

sheet["A1"].alignment.vertical = "top"

OTHER TIPS

This works for me. I am using openpyxl v2.5.8.

new_cell.alignment=Alignment(horizontal='general',
                     vertical='top',
                     text_rotation=0,
                     wrap_text=False,
                     shrink_to_fit=False,
                     indent=0)

For more info: https://openpyxl.readthedocs.io/en/stable/styles.html?highlight=cell%20alignment

Use two loops to apply the alignment format to each cell.

al = Alignment(horizontal="left", vertical="top")
for row in sheet['A1':'A2']:
    for cell in row:
        cell.alignment = al

This idea comes from https://groups.google.com/forum/#!topic/openpyxl-users/GDrfknwrYEM

You set the cell style.alignment.vertical to a desired value. For example, to make the cell A1 to vertically align up:

# ws is worksheet
myCell = ws.cell('A1')
myCell.style.alignment.vertical = Alignment.VERTICAL_TOP

Find more details on the class reference page here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top