[Python] openpyxl writing multiple headers and content inside the same sheet

Discussão em 'Python' iniciado por Stack, Junho 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using openpyxl for the first time in my django project. I got the task to make a two-part excel, that should be looking approximately like this:

    [​IMG]

    So basically i want to show all users of a certain organization. This is what I have done so far:

    def export_organisation(self, org_id):
    organization = Organization.objects.get(pk=org_id)

    wb = Workbook()
    ws = wb.active
    ws.title = "OKR"

    headers = [
    ['Organisation Name', 'Signup Date', 'Language'],
    ['First Name', 'Last Name', 'Email']
    ]

    line = 1 # Start from the first row

    for header_row in headers:

    ws.append(header_row)

    # Format bold the cells in the first row
    ft_bold = Font(bold = True)
    for cell in ws[ws.max_row]:
    cell.font = ft_bold

    line = ws.max_row+1

    ws['A' + str(line)] = organization.name
    ws['B' + str(line)] = organization.signup_date.strftime("%d/%m/%Y")
    ws['C' + str(line)] = organization.language



    for user in organization.users.all():

    ws['D' + str(line)] = user.first_name
    ws['E' + str(line)] = user.last_name
    ws['F' + str(line)] = user.email
    line+=1

    # Filename with team name and date
    wb.save('{}.xlsx'.format(org_id))


    But this gives me following output:

    [​IMG]

    Is there a way to make it look like the first image? If yes, what did I miss?

    Continue reading...

Compartilhe esta Página