When using pl.write_excel
, I am looking for a possibility to rotate SOME header columns by 90°.
I am applying a bunch of input arguments provided by pl.write_excel
in order to style the exported dataframe. Among others, I am using header_format
.
df.write_excel(header_format={"rotation": 90}
This will rotate all header columns by 90°. However, I am looking for a way to rotate only the last three header columns.
It ain't much, but it's honest work:
df = pl.DataFrame({
'A long name': [1,2,3],
'Be my guest': [4,5,6],
'Come in and find out': ['a', 'b', 'c'],
'Dont stop me now': ['d', 'e', 'f'],
'Easy stuff only': ['g', 'h', 'i'],
})
df1 = df.select(df.columns[:-3])
df2 = df.select(df.columns[-3:])
position_df2 = chr(ord('@') + len(df.columns[:-3])+1) + "1"
with xlsxwriter.Workbook('workbook.xlsx') as workbook:
df1.write_excel(workbook=workbook, worksheet="Sheet1")
df2.write_excel(workbook=workbook, worksheet="Sheet1", header_format={"rotation": 90}, position=position_df2)
Basically you split your dataframe at the desired position, then calculate the length of the first part to see where to attach the second part. By doing so you can format the second dataframe just as you did.