Perché non posso “salva come” un file Excel dal mio codice Python?
Domanda
Ho una classe ExcelDocument
Python che fornisce metodi di convenienza di base per la lettura / scrittura / formattazione file di Excel, e sto ottenendo uno strano errore nel apparentemente semplice codice Python. Ho un metodo Save e saveAs
:
def save(self):
''' Save the file '''
self.workbook.Save()
def saveAs(self, newFileName):
''' Save the file as a new file with a different name '''
self.workbook.SaveAs(newFileName)
Il metodo di salvataggio funziona perfettamente, ma quando provo a chiamare il metodo saveAs
- myExcelObject.saveAs("C:/test.xlsx")
- ottengo il seguente errore:
Traceback (most recent call last):
File "C:\workspace\Utilities\src\util\excel.py", line 201, in <module>
excel.saveAs("C:/test.xlx")
File "C:\workspace\Utilities\src\util\excel.py", line 185, in saveAs
self.workbook.SaveAs(newFileName)
File "<COMObject Open>", line 7, in SaveAs
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office Excel', u"Microsoft Office Excel cannot access the file 'C:\\//8CBD2000'. There are several possible reasons:\n\n\u2022 The file name or path does not exist.\n\u2022 The file is being used by another program.\n\u2022 The workbook you are trying to save has the same name as a currently open workbook.", u'C:\\Program Files\\Microsoft Office\\Office12\\1033\\XLMAIN11.CHM', 0, -2146827284), None)
Qualcuno può spiegare che cosa sta accadendo?
Soluzione
Ho trovato (nel modo più duro), che SaveAs
non supporta /
barra.
Prova saveAs("C:\\test.xlx")
invece.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow