Pywintypes.com_error: (-2147352567, ' unexpected solution occurred: appearing in Python call WPS __python

Source: Internet
Author: User

These days to do a small program, the main function is to use the Win32com library to invoke WPS, and the use of some simple two development instructions, to achieve the transformation of file format and save as a new file.

The approximate code is as follows:

Import win32com
import win32com.client
w=win32com.client.constants
wps=win32com.client.gencache. (' Kwps.application ')
d=wps. Documents.Open (R ' C:\Users\asus\Desktop\project\ document 2.docx ')
d.saveas2 (filename=r ' C:\Users\asus\Desktop\ project\ document text. txt ', Fileformat=w.wdformattext

Program to run an error:

Pywintypes.com_error: (-2147352567, ' accident occurred. ', (0, ' Kingsoft WPS ', ' document save failed. ', ', 3011, -2147467259, None)

Looking for a long time on the internet, and finally in a blog to see someone using the

Win32com.client.gencache.EnsureDispatch

Loaded WPS, it was used in my program, but the blogger also put forward: "Do not know the reason for the solution." "After I used the ensuredispatch, the problem happened to be solved" (continue to read), the code is as follows:

Import win32com
import win32com.client
w=win32com.client.constants
wps= Win32com.client.gencache.EnsureDispatch (' kwps.application ')
d=wps. Documents.Open (R ' C:\Users\asus\Desktop\project\ document 2.docx ')
d.saveas2 (filename=r ' C:\Users\asus\Desktop\ project\ document text. txt ', Fileformat=w.wdformattext

For some good luck, the file was saved successfully. I thought the problem was solved until the program had been submitted, thinking and trying today to discover that the problem was not solved at all.


After my repeated attempts, I found:

The cause of the problem is not the way of access or any other reason, but a very simple reason: The current program does not have permission to target files. In other words, the WPS we are mounting does not have permission to save as this opened document. Continue to delve into the reason is simple and very funny:

The WPS that was previously opened has not been closed.

At this time, it is natural to have problems. In the adoption of Ensuredispatch at the same time, I happened to manually shut down the WPS process, and then found that the success of the operation did not try again, but continue to write a subsequent shutdown of the function of the loading program, so the problem has never appeared ... That I thought it was "settled".

Attach the code to close WPS:

Def closesoft ():
    print (' ' Mount program off ...

          ')
    Import win32com
    import win32com.client
    wc=win32com.client.constants

    try:
        wps= Win32com.client.gencache.EnsureDispatch (' kwps.application ')
    except:
        wps= Win32com.client.gencache.EnsureDispatch (' wps.application ')
    else:
        wps= Win32com.client.gencache.EnsureDispatch (' Word.Application ')
    try:
        wps. Documents.close ()
        wps. Documents.close (wc.wddonotsavechanges)
        wps. Quit
    Except:pass

It can close two kinds of WPS and Ms WORD, and can also close the PPT and et with a slight modification.

Although the end does not really affect the use of the program, but the problem still exists, write down, you can later know why.

Related Article

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.