Skip to content

Instantly share code, notes, and snippets.

@dmvianna
Last active December 10, 2015 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmvianna/4499821 to your computer and use it in GitHub Desktop.
Save dmvianna/4499821 to your computer and use it in GitHub Desktop.
Testing XLpandas.py
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 10 15:14:02 2013
@author: daniel.vianna
"""
from copy import deepcopy
from xlwt import Worksheet
import numpy as np
import pandas as pd
from xlwt import Workbook
conv = pd.io.parsers.CellStyleConverter()
hstyle_dict = {"font": {"bold": True},
"border": {"top": "thin",
"right": "thin",
"bottom": "thin",
"left": "thin"},
"align": {"horiz": "center"}}
hstyle = conv.to_xls(hstyle_dict)
t = pd.DataFrame(np.array(np.mat('0 1 0 1; 1 0 2 3; 1 1 2 4')))
arrays = [['a','a','b','b'],['one','two','one','two']]
tuples = zip(*arrays)
index = pd.MultiIndex.from_tuples(tuples, names=['First','Second'])
t.columns = index
t.index = index[:3]
t2 = XLtable(deepcopy(t))
t1 = XLtable(deepcopy(t))
t1.index = arrays[1][:3]
t1.columns = arrays[1]
t1.row_depth = 1
t1.col_depth = 1
s = pd.Series(np.array([1, 1, 2, 4]))
s.index = index
d = XLseries(s)
d1 = deepcopy(d)
d1.index = ['a','a','b','b']
d1.index_depth = 1
wb = xlwt.Workbook()
ws_1 = wb.add_sheet('simple index', cell_overwrite_ok=False)
ws_2 = wb.add_sheet('multi index', cell_overwrite_ok=False)
ws_3 = wb.add_sheet('series index', cell_overwrite_ok=False)
t1.place_index(ws=ws_1, row=5, col=0, axis=0)
t1.place_index(ws=ws_1, row=0, col=5, axis=1)
t1.place_table(ws=ws_1, row=5, col=5)
t2.place_index(ws=ws_2, row=5, col=0, axis=0)
t2.place_index(ws=ws_2, row=0, col=5, axis=1)
t2.place_table(ws=ws_2, row=5, col=5, cstyle=hstyle)
d.place_index(ws=ws_3, row=5, col=0, axis=0)
d.place_index(ws=ws_3, row=2, col=5, axis=1)
d1.place_index(ws=ws_3, row=0, col=5, axis=1)
d1.place_index(ws=ws_3, row=5, col=2, axis=0)
d.place_data(ws=ws_3, row=5, col=5, axis=1)
d.place_data(ws=ws_3, row=7, col=5, axis=0)
d.place_series(ws=ws_3, row=12, col=0, axis=0)
d.place_series(ws=ws_3, row=12, col=4, axis=1)
d1.place_series(ws=ws_3, row=12, col=10, axis=0, istyle=hstyle)
d1.place_series(ws=ws_3, row=12, col=14, axis=1, dstyle=hstyle)
wb.save('test.xls')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment