pythonexport-to-excelxlsxwriter

Change orientation of table Header using xlsxwriter


I am exporting a quite large table (40 rows including header; 69 columns)

The majority of the columns are just numbers so I set the width to, e.g., 5.

This results in unreadable columns headers. Now I want to Rotate them 90 degree clockwise weithinpython/xlsxwriter. During my Research I found This topic Orient text in cell to vertical with XlsxWriter? but unfortunately it results in a corrupt file which Excel offers to repair on opening. I tried the Position 'A1' which is the Header of the first column.

I even looked into the xlsxwriter docs but IT seems there's no support for this yet (or I am looking AT the wrong) places.

Do you have an ideal how to achieve the desired result?

The following MWE does (in contrast to my previous attempts) rotate the desired cell but due to Excels repair the previous title is set to Column1.

I even tried to 'insert' into the loop where I create the columns but this has no effect (and no error).

data = pd.DataFrame(
    [[1, 2], [3, 4]], columns=["short", "Veeeeeeeeeeeeeeeeeeeeeeeeery long"]
)
with pd.ExcelWriter("MWE.xlsx", engine="xlsxwriter") as writer:
    workbook = writer.book
    rotate_format = workbook.add_format({"rotation": -90})
    worksheet = workbook.add_worksheet("MWE")
    (cols, rows) = data.shape
    col_settings = [
        {
            "header": column,
        }
        for column in data.columns
    ]
    worksheet.add_table(
        0,
        0,
        rows,
        cols - 1,
        {
            "columns": col_settings,
            "banded_rows": True,
        },
    )
    data.to_excel(
        writer,
        index=False,
        sheet_name="MWE",
    )
    worksheet.write("B1", None, rotate_format)

Solution

  • I think I found the issue: The df.to_excel() function seems to interfere with the worksheet.add_table()-function when setting the header_format.

    With https://xlsxwriter.readthedocs.io/working_with_tables.html#columns I got the right hints (column_format instead of format etc).

    The following works as intended and this procedure solves other issues I have not adressed yet (formulas etc):

    data = pd.DataFrame(
        [[1, 2], [3, 4]], columns=["short", "Veeeeeeeeeeeeeeeeeeeeeeeeery long"]
    )
    with pd.ExcelWriter("MWE.xlsx", engine="xlsxwriter") as writer:
        workbook = writer.book
        rotate_format = workbook.add_format({"rotation": -90})
        worksheet = workbook.add_worksheet("MWE")
        (cols, rows) = data.shape
        col_settings = [
            {
                "header": column,
            }
            for column in data.columns
        ]
        worksheet.add_table(
            0,
            0,
            rows,
            cols - 1,
            {
                "data": data.values.tolist(),
                "banded_rows": True,
                # 'columns': col_settings,
                "columns": [
                    {
                        "header": "short",
                    },
                    {
                        "header": "veeeeeeeeeeeeeeeeeeeeeeeeeery long",
                        "header_format": rotate_format,
                    },
                ],
            },
        )