Skip to content

Instantly share code, notes, and snippets.

@den-run-ai
Created September 6, 2016 20:00
Show Gist options
  • Save den-run-ai/ec559b5af41060c5ac318f7f59d8b415 to your computer and use it in GitHub Desktop.
Save den-run-ai/ec559b5af41060c5ac318f7f59d8b415 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied (use --upgrade to upgrade): pythonnet in c:\\python\\python27_32b\\lib\\site-packages\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"You are using pip version 8.1.1, however version 8.1.2 is available.\n",
"You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n"
]
}
],
"source": [
"!pip install pythonnet"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import clr\n",
"import sys\n",
"clr.AddReference(\"Microsoft.Office.Interop.Excel\")\n",
"import Microsoft.Office.Interop.Excel as Excel"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Excel: <class 'Microsoft.Office.Interop.Excel.ApplicationClass'>\n",
"Workbooks: <class 'System.__ComObject'>\n"
]
}
],
"source": [
"excel = Excel.ApplicationClass()\n",
"excel.Visible = True # makes the Excel application visible to the user - will use this as True for debug\n",
"excel.DisplayAlerts = False # turns off Excel alerts since I don't have a handler\n",
"\n",
"print (\"Excel: \" + str(type(excel)))\n",
"print (\"Workbooks: \" + str(type(excel.Workbooks)))\n",
"#print (\"Workbooks count: \" + str(excel.Workbooks.Count))\n",
"#wb = excel.Workbooks.Open(r'C:\\Projects\\Experiments\\Python\\ExcelInterop\\Test.xlsx')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"clr.AddReference(\"System.Reflection\")\n",
"from System.Reflection import BindingFlags\n",
"\n",
"excel.Workbooks.GetType().InvokeMember(\"Count\",\n",
" BindingFlags.GetProperty | BindingFlags.InvokeMethod , \n",
" None, excel.Workbooks, None)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"System.Object InvokeMember(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object, System.Object[])\n",
"System.Object InvokeMember(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object, System.Object[], System.Globalization.CultureInfo)\n",
"System.Object InvokeMember(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object, System.Object[], System.Reflection.ParameterModifier[], System.Globalization.CultureInfo, System.String[])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"excel.Workbooks.GetType().InvokeMember.__overloads__"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<System.__ComObject at 0x7db1810>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"excel.Workbooks.GetType().InvokeMember(\"Add\",BindingFlags.InvokeMethod, None, excel.Workbooks, None)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"isinstance(excel.Workbooks, clr.System.__ComObject)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class comobj(object):\n",
" def __init__(self, obj):\n",
" #AttributeError: _comobj__ComObject???\n",
" #if not isinstance(obj, clr.System.__ComObject):\n",
" # raise TypeError('Type not System.__ComObject for obj {}'.format(type(obj)))\n",
" self.obj = obj\n",
" self.typ = obj.GetType()\n",
" def __getattr__(self, name): \n",
" def newm(*argsv):\n",
" if not argsv:\n",
" argsv=(None,)\n",
" return self.typ.InvokeMember(name,BindingFlags.InvokeMethod | BindingFlags.GetProperty, None, self.obj, *argsv)\n",
" return newm"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"co1=comobj(excel.Workbooks)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<System.__ComObject at 0x31885b0>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"co1.obj"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<System.__ComObject at 0x7db1730>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"co1.Add()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"co1.Count()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from System import Array\n",
"import System"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"arrobj=Array[System.Object]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"arr1=arrobj.CreateInstance(System.Object,1)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"arr1[0]=r\"C:\\booktest.xlsx\""
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[u'C:\\\\booktest.xlsx']"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(arr1)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<System.__ComObject at 0x7e81610>"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"co1.Open(arr1)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"co1.Close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
},
"latex_envs": {
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 0
},
"widgets": {
"state": {},
"version": "1.1.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment