Python:Excel即使使用\\也无法打开文件

时间:2018-03-04 15:03:21

标签: python excel path directory win32com

我遇到了错误:

      File "C:\Python27\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x9\Workbooks.py", line 78, in Open
    , Converter, AddToMru, Local, CorruptLoad)
com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', ...

然后经过一些搜索我发现问题是/,com32不接受。最后我能够用\\更改/,但错误仍然存​​在。有人能帮助我吗?

代码:

def WriteValues():
    global ws
    global ws2
    global wb
    global wb2
    global filename

    Row = 2
    MaxRow = 9000
    while Row < MaxRow:
        if ws["A" + str(Row)] != "":
            Copy = copyRange(1,Row,1000,Row,ws)
            Paste = pasteRange(1,2,1000,2,ws2,Copy)
            Email = ws["C" + str(Row)]
            wb2.save(filename)
            wb2.close()
            office = win32com.client.gencache.EnsureDispatch('Excel.Application')
            office.Application.Quit()
            office = win32com.client.gencache.EnsureDispatch('Excel.Application')
            print filename
            filename2 = str(os.path.normpath(filename))
            filename2 = (filename2.encode('string-escape'))
            print filename2
            wb2 = office.Workbooks.Open(filename2)
            wb2.RefreshAll()
            wb2.Save()
            wb2.Close()
            Print()
            SendEmail(Needs[0],Needs[1],Needs[2],Needs[3],Needs[4],Email)
            Row = Row + 1

输出:

=============== RESTART: C:\Users\macha\Desktop\Excel\Code.py ===============
C:/Users/macha/Desktop/Excel/RelatorioDeDesempenho.xlsx
C:\\Users\\macha\\Desktop\\Excel\\RelatorioDeDesempenho.xlsx

Traceback (most recent call last):
  File "C:\Users\macha\Desktop\Excel\Code.py", line 428, in <module>
    WriteValues()
  File "C:\Users\macha\Desktop\Excel\Code.py", line 418, in WriteValues
    wb2 = office.Workbooks.Open(filename2)
  File "C:\Python27\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x9\Workbooks.py", line 78, in Open
    , Converter, AddToMru, Local, CorruptLoad)
com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u'O m\xe9todo Open da classe Workbooks falhou', u'xlmain11.chm', 0, -2146827284), None)

PS:我的电脑是葡萄牙语......

0 个答案:

没有答案