I am trying to export a pandas dataframe to excel using win32.
The export seems to work only when the dataframe does not include numpy datatypes.
How can I convert numpy datatypes to their COM variant ?
Here a reproducible example :
import pandas as pd
from win32com.client import Dispatch
# Prepare data
# data without numpy data type
xdata1 = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
'year' : [2000, 2001, 2002, 2002, 2001],
'pop' : [1.5, 1.7, 3.6, 2.4, 2.9 ]}
data1 = pd.DataFrame(xdata1, columns = ['year', 'state', 'pop'])
xT1 = [tuple(x) for x in data1.values]# # data to tuples
# data with numpy type
data2 = pd.crosstab(data1.state, data1.year)
xT2 = [tuple(x) for x in data2.values] # data to tuples
# export the data
from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlApp.Visible = 1
xlApp.Workbooks.Add()
xlSheet = xlApp.ActiveWorkbook.ActiveSheet
# write to excel
xlSheet.Cells(1,1).Value = 'Python Rules!' # THIS WORKS AS EXPECTED
# Write to a range : data without numpy data type
FirstRow = 2
FirstCol = 3
LastRow = FirstRow + len(xT1) - 1 # Number of records
LastCol = FirstCol + len(xT1[0]) - 1 # Number of columns
xlSheet.Range(xlSheet.Cells(FirstRow, FirstCol), xlSheet.Cells(LastRow , LastCol)).Value = xT1 # THIS WORKS AS EXPECTED
# Write to a range : data WITH numpy data type
FirstRow = 2
FirstCol = 5
LastRow = FirstRow + len(xT2) - 1 # Number of records
LastCol = FirstCol + len(xT2[0]) - 1 # Number of columns
xlSheet.Range(xlSheet.Cells(FirstRow, FirstCol), xlSheet.Cells(LastRow , LastCol)).Value = xT2
THIS LINE PRODUCES AN ERROR
TypeError: Objects of type 'numpy.int64' can not be converted to a COM VARIANT (but obtaining the buffer() of this object could)
P.S: before someone asks (or suggest another way to export to export). I am using win32 on purpose because it is the only way I found to write to existing, pre-prepared excel file and keep the styles
P.P.S : Inspired by this posting, a solution I came up with is to change the type of each single element of the dataframe I am sure there are better ways
def fnConvertdType (xListofList):
newListofList = []
for itemList in xListofList:
xList = []
for item in itemList:
xList.append(np.asscalar(np.int16(item)))
newListofList.append(xList)
return newListofList