문제

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

도움이 되었습니까?

해결책 2

Try this:

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top