“com_error:( - 2147417851,'服务器抛出异常。',无,无)”

时间:2013-11-19 09:28:15

标签: excel python-2.7 pywin32

我需要从三个excel文件中复制数据&粘贴到三个不同的Excel文件中。

Error:
     ('F', 'C:\\Users\\jmp655972\\Desktop\\SOFTWARE DEVELOPMENT\\Folder Creation               Format\\Base Vehicle Laden\\Steady State\\Continous Test Data\\LH Turn\\test_1.csv')
Pass
('F', 'C:\\Users\\jmp655972\\Desktop\\SOFTWARE DEVELOPMENT\\Folder Creation Format\\Base Vehicle Laden\\Steady State\\Continous Test Data\\LH Turn\\test_2.csv')

Traceback (most recent call last):
  File "C:\Users\jmp655972\Desktop\Handling_Analysis_Software\main.py", line 120, in <module>
    filescheck(fileCount)
  File "C:\Users\jmp655972\Desktop\Handling_Analysis_Software\main.py", line 115, in filescheck
    readFilesAndCopyData(excelObj,"roll","1",csvfile)
  File "C:\Users\jmp655972\Desktop\Handling_Analysis_Software\main.py", line 62, in readFilesAndCopyData
    workbook = excel.Workbooks.Open(filetoOpen)
  File "C:\Python27\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x6.py", line 34940, in Open
    , Converter, AddToMru, Local, CorruptLoad)
com_error: (-2147417851, 'The server threw an exception.', None, None

def openExcel():
excel = client.Dispatch("Excel.Application")
excel.Visible = 1
return excel;



def readFilesAndCopyData(excel,testtype,vehicletype,filetoOpen):
    currworkingdirectory = os.getcwd()
    print ("F",filetoOpen)
    workbook = excel.Workbooks.Open(filetoOpen)
    worksheets = workbook.Worksheets(1)
    if testtype == "roll":
        if vehicletype == "1":
            print ("Pass")
            worksheets.Range("E:E,H:H,I:I").Select()
            worksheets.Range("E:E,H:H,I:I").Copy()
        else:
            worksheets.Range("L:L,O:O,P:P").Select()
            worksheets.Range("L:L,O:O,P:P").Copy()
    elif testtype == "slip":
        if vehicletype == "1":
            worksheets.Range("E:E,G:G").Select()
            worksheets.Range("E:E,G:G").Copy()
        else:
            worksheets.Range("E:E,G:G").Select()
            worksheets.Range("E:E,G:G").Copy()
    else:
        print("No Test Data Found")

    workbook.Close
    pasteCopiedData(excel)
    counter + 1 

1 个答案:

答案 0 :(得分:-1)

在尝试自动执行Excel工作时出现了相同的错误消息。具体来说,应该打开Excel文件的一行代码引发了此错误。原因似乎是文件路径(目录+文件名)太长的事实。重命名文件夹和文件名以使其更短时,该错误消失了。