Pywintypes.com_error: (-2147221008, ' CoInitialize not yet called. ', none, none)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.