Skip to content

Instantly share code, notes, and snippets.

@cwebber314
Last active January 10, 2018 13:08
Show Gist options
  • Save cwebber314/9907006 to your computer and use it in GitHub Desktop.
Save cwebber314/9907006 to your computer and use it in GitHub Desktop.
Example of using XLPandas to write a pretty dataframe to excel
"""
pandas 0.12.0
"""
from numpy.random import randn
import numpy as np
import xlwt
import pandas as pd
from xlpandas import XLtable, XLseries
from xlwt import Workbook, Worksheet, Style
import os
cstyle = xlwt.easyxf("font: bold True; border: top thin, right thin, bottom thin, left thin; align: horiz center")
rstyle = xlwt.easyxf("font: bold True; border: top thin, right thin, bottom thin, left thin; align: horiz center", num_format_str='DD/MM/YYYY')
dstyle = xlwt.easyxf("font: name Courier", num_format_str='#,##0')
ser = pd.Series([1,2,3,4])
ser1 = XLseries(ser)
dates = pd.date_range('1/1/2000', periods=8)
df = pd.DataFrame(randn(8, 4)*1e9, index=dates, columns=['A', 'B', 'C', 'D'])
wb = Workbook()
ws_1 = wb.add_sheet('sheet_foo', cell_overwrite_ok=False)
df1 = XLtable(df)
df1.place_table(ws_1, rstyle=rstyle, cstyle=cstyle, dstyle=dstyle)
wb.save('test.xls')
os.startfile('test.xls')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment