win32com.client self terminating issue

北战南征 提交于 2020-01-06 10:56:12

问题


I am using Python 3.3.5 and trying to save an Excel 2003 file (.xls) as Excel 2007 file (.xlsx). The problem with the following scripts is that the script is working very well if I run it inside of Spyder, however if I try to run it simply double-clicking on script, it does not working.

Spyder can import win32com.client with no problem and run the script successfully while IDLE can not run the script and gives that error:

"import win32api, sys, os "

"ImportError: DLL load failed: The specified module could not be found."

Excel_File_Conversion Script through win32com.client

fname = filedialog.askopenfilename(filetypes=(("Excel files", "*.xls;*.xlsx"),
                                              ("All files", "*.*") ))
fname = fname.replace("/",os.path.sep)
if fname[-1] != 'x':
    try:
        import win32com.client as win32 
        excel = win32.gencache.EnsureDispatch('Excel.Application')
        wb = excel.Workbooks.Open(fname)
        messagebox.showinfo(title = "Conversion",
                            message="Excel 2003(.xls) format was converted to Excel 2007(.xlsx) format",
                            detail = "Press OK to continue")
        wb.SaveAs(fname+"x", FileFormat = 51)
        wb.Close()
        excel.Application.Quit()
        fname = fname+"x"
    except TypeError:
        messagebox.showerror(title = "Error", message="File could not be opened")

PS: I have no problem with running scripts by double clicking.


回答1:


Unfortunately, pywin32 doesn't always install correctly. There's a few things you can try (in this order):

  • In a command prompt (you may have to right-click and "Run as Administrator") run python C:\Python33\Scripts\pywin32_postinstall.py -install

  • Add the path of where you find the pythoncom33.dll to your Windows PATH. It might be under the Python root directory or further down.

  • Copy the pythoncom33.dllto C:\Python33\Lib\site-packages\win32

  • Otherwise you might want to try one of the distributions that already come with pywin32included like Anaconda or Canopy or WinPython

Always adjust your path accordingly.



来源:https://stackoverflow.com/questions/24472615/win32com-client-self-terminating-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!