2016-09-30 64 views
0

我想通过win32com打开excel文件。但是,当我用外语(韩语)语言运行代码时,它会给出一个错误,而英文文件名不会发生。通过python win32com打开excel文件,其中有外文文件名

我该如何解决这个问题?

#-*- coding: utf-8 -*- 
import os 
import win32com.client 

xl=win32com.client.Dispatch("Excel.Application") 
xl.Visible = True 

xl.Workbooks.Open(os.path.join(os.getcwd(), "Nikkei225_10월.xlsm")) 

xl.Application.Quit() # Comment this out if your excel script closes 
del xl 

下面是一个错误消息:

com_error: (-2147352567, '\xbf\xb9\xbf\xdc\xb0\xa1 
\xb9\xdf\xbb\xfd\xc7\xdf\xbd\xc0\xb4\xcf\xb4\xd9.', (0, u'Microsoft Office 
Excel', u"'C:\\Users\\Jongho\\dev_jhk\\VNI Automation Pilot Test\\Nikkei225_10?? 
xlsm.xlsx'\uc744(\ub97c) \ucc3e\uc744 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4. 
\ud30c\uc77c \uc774\ub984\uc758 \ucca0\uc790\uc640 \ud30c\uc77c 
\uc704\uce58\uac00 \uc815\ud655\ud55c\uc9c0 
\ud655\uc778\ud558\uc2ed\uc2dc\uc624.\n\n\ucd5c\uadfc\uc5d0 \uc0ac\uc6a9\ud55c 
\ud30c\uc77c \ubaa9\ub85d\uc5d0 \uc788\ub294 \ud30c\uc77c\uc744 
\uc5f4\ub824\uba74 \ud30c\uc77c\uc758 \uc774\ub984\uc774 
\ubcc0\uacbd\ub418\uac70\ub098 \ud30c\uc77c\uc774 \uc774\ub3d9 \ub610\ub294 
\uc0ad\uc81c\ub418\uc9c0 \uc54a\uc558\ub294\uc9c0 
\ud655\uc778\ud558\uc2ed\uc2dc\uc624.", u'C:\\Program Files\\Microsoft 
Office\\Office12\\1042\\XLMAIN11.CHM', 0, -2146827284), None) 

回答

0

这是一个编码的问题。

#-*- coding: utf-8 -*- 
import os 
import win32com.client 

xl=win32com.client.Dispatch("Excel.Application") 
xl.Visible = True 

xl.Workbooks.Open("C:\\Users\\Jongho\\dev_jhk\\VNI Automation Pilot Test\\Nikkei225_10\xbf\xf9.xlsm") 

xl.Application.Quit() # Comment this out if your excel script closes 
del xl