1

I'm attempting to write rotated text to an Excel file from a Pandas dataframe. However I'm unable to rotate text using pandas ExcelWriter set_rotation() method.

I was able to do this using xlsxwriter directly, but have been unable to do the same thing via pandas.

import pandas as pd
from pathlib import Path

df = pd.DataFrame({"Letters": ["A", "B", "C"], "Numbers": [1, 2, 3]})
writer = pd.ExcelWriter(f'{str(Path.home())}/Desktop/test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
wb  = writer.book
ws = writer.sheets['Sheet1']
format_rotation = wb.add_format({})
format_rotation.set_rotation(90)
ws.set_row(0, 100, format_rotation)
wb.close()
writer.save()

I expect "Letters" and "Numbers" to be formatted to 90 degrees when the Excel file is opened.

Stuber
  • 447
  • 5
  • 16

0 Answers0