python-2.7pandasstyleframe

Changing column width from excel files


I have a file like so that I am reading from excel:

Year  Month  Day
1     2      1
2     1      2

I want to specify the column width that excel recognizes. I would like to do it in pandas but I don't see a option. I have tried to do it with the module StyleFrame.

This is my code:

from StyleFrame import StyleFrame
import pandas as pd

df=pd.read_excel(r'P:\File.xlsx')
excel_writer = StyleFrame.ExcelWriter(r'P:\File.xlsx')
sf=StyleFrame(df)
sf=sf.set_column_width(columns=['Year', 'Month'], width=4.0)
sf=sf.set_column_width(columns=['Day'], width=6.00)
sf=sf.to_excel(excel_writer=excel_writer)
excel_writer.save()

but the formatting isn't saved when I open the new file.

Is there a way to do it in pandas? I would even take a pure python solution to this, pretty much anything that works.


Solution

  • As for your question on how to remove the headers, you can simply pass header=False to to_excel:

    sf.to_excel(excel_writer=excel_writer, header=False).

    Note that this will still result with the first line of the table being bold. If you don't want that behavior you can update to 0.1.6 that I just released.