【发布时间】:2014-07-01 05:49:58
【问题描述】:
我正在使用 Python 3.3.5 并尝试将 Excel 2003 文件 (.xls) 保存为 Excel 2007 文件 (.xlsx)。以下脚本的问题是,如果我在 Spyder 中运行它,脚本运行良好,但是如果我尝试运行它,只需双击脚本,它就不起作用。
Spyder 可以毫无问题地导入 win32com.client 并成功运行脚本,而 IDLE 无法运行脚本并给出错误:
"导入 win32api, sys, os"
“ImportError: DLL load failed: 找不到指定的模块。”
Excel_File_Conversion 脚本通过 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:双击运行脚本没问题。
【问题讨论】:
-
您是否在安装了 Spyder 的同一台计算机上运行脚本?听起来您需要安装pywin32。尝试从这个位置安装它:lfd.uci.edu/~gohlke/pythonlibs/#pywin32
-
是的,肯定是同一台电脑,奇怪的是脚本开始运行,然后突然终止,没有给出任何错误。
-
在 Spyder 中,您可以选择在外部终端中运行脚本(使用 F6 更改您的偏好),看看是否有帮助。
标签: python excel win32com spyder