1. When you use multithreaded programming, and you need to call the Win32com module to open a Word document, common errors are as follows:
IDispatch = pythoncom. CoCreateInstance (IDispatch, None, Clsctx, pythoncom. IID_IDispatch) Com_error: ( -2147221008, ' \xc9\xd0\xce\xb4\xb5\xf7\xd3\xc3 coinitialize\xa1\xa3 ', none, none)
The second item in the second line of error is printed: "CoInitialize not yet called", then add the following two lines of code in the code to resolve:
Import pythoncompythoncom. CoInitialize ()
So what is pythoncom used for?
In URL http://docs.activestate.com/activepython/2.4/pywin32/pythoncom.html, the interpretation of the CoInitialize () function of the query pythoncom is:
Initialize the COM libraries for the calling thread.
2. Read the contents of the Word document, the common mistake is, when reading English, no problem, but encountered in Chinese, will error, see the following code:
1 Import win32com2 from win32com.client import Dispatch3 msword = Dispatch (' Word.Application ') 4 msword. Visible = MSWord. DisplayAlerts = doc = MSWord. Documents.Open (filename=u "J:\\study.docx", encoding= ' GB18030 ') 7 range = Doc. Range (Doc. Content.start, Doc. Content.end) 8 text = range.__str__ ()
The errors that occur are:
Unicodeencodeerror: ' ASCII ' codec can ' t encode characters in position 0-18:ordinal not in range (128)
Modified to Unicode (range), or error, and then modified to Text = Unicode (range), only to be resolved, but the specific reason has not been proved.
This article is from my blog blog, so be sure to keep this source http://10552290.blog.51cto.com/10542290/1881213
Pywintypes.com_error: (-2147221008, ' CoInitialize not yet called. ', none, none)