2

How to automate Microsoft office using office interop with python.net? I tried following but gives error:

import clr
clr.AddReference("System")
clr.AddReference("Microsoft.Office.Interop")

from Microsoft.Office.Interop import Word

wordApp = Word.Application()
wordApp.Visible = True
doc1 = wordApp.Documents.Add()

AttributeError: '__ComObject' object has no attribute 'Add'

Rahul
  • 10,830
  • 4
  • 53
  • 88
  • In an old C# project, I have `Word.Document doc = wordApp.Documents.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);`. The four parameters became optional in C# 4.0, around 2010, which was meant to make COM calls much easier, e.g. `Word.Document doc = wordApp.Documents.Add();`, but this feature may not yet be supported in Python.NET. Possibly related: http://stackoverflow.com/questions/26742837/creating-a-c-sharp-nullable-int32-within-python-using-python-net-to-call-a-c-s – Nat Dec 29 '16 at 10:17

1 Answers1

1

I wrote a simple wrapper that enables COM interop with pythonnet. Full details here:

https://github.com/pythonnet/pythonnet/issues/260

denfromufa
  • 5,610
  • 13
  • 81
  • 138