问题
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 -installAdd the path of where you find the
pythoncom33.dllto your Windows PATH. It might be under the Python root directory or further down.Copy the
pythoncom33.dlltoC:\Python33\Lib\site-packages\win32Otherwise you might want to try one of the distributions that already come with
pywin32included likeAnacondaorCanopyorWinPython
Always adjust your path accordingly.
来源:https://stackoverflow.com/questions/24472615/win32com-client-self-terminating-issue