问题描述
我已经检查了许多不同的选项来打开一个已知密码的 Excel 文件,但我一直无法做到。
我创建了一个 Excel 文件“test_file.xlsx”并使用密码“123”保存它。我知道那里的内容,我想通过 Python 操作它们。这些是我咨询过的主要来源,但没有一个对我有用:
From password-protected Excel file to pandas DataFrame
https://thenethawks.com/python-from-password-protected-excel-file-to-pandas-dataframe/
我正在使用 Jupyter Notes (Anaconda):
import win32com.client
import csv
import sys
import pandas as pd
from tempfile import NamedTemporaryFile
xlApp = win32com.client.dispatch("Excel.Application")
filename,password = r"test_file.xslx","123"
# Note this line from the question posted
xlwb = xlApp.Workbooks.Open(filename,False,True,None,password)
xlws = xlwb.Sheets(1) # index is from 1
print (xlws.Name)
print (xlws.Cells(1,1)) # if you need cell values
f = NamedTemporaryFile(delete=False,suffix='.csv')
f.close()
os.unlink(f.name)
xlCSVWindows = 0x17 # CSV file format,from enum XlFileFormat
xlwb.SaveAs(Filename=f.name,FileFormat=xlCSVWindows) # Save as CSV
df = pd.read_csv(f.name)
print(df.head())
df.to_csv("myoutput.csv",index=False)
---------------------------------------------------------------------------
com_error Traceback (most recent call last)
<ipython-input-38-a529ec4251c6> in <module>
31
32 # Note this line from the question posted
---> 33 xlwb = xlApp.Workbooks.Open(filename,password)
34
35 xlws = xlwb.Sheets(1) # index is from 1
~\AppData\Local\Temp\gen_py\3.8\00020813-0000-0000-C000-000000000046x0x1x9.py in Open(self,Filename,UpdateLinks,ReadOnly,Format,Password,WriteResPassword,IgnoreReadOnlyRecommended,Origin,Delimiter,Editable,Notify,Converter,AddTomru,Local,CorruptLoad)
41187,Editable=defaultNamedOptArg,Notify=defaultNamedOptArg,Converter=defaultNamedOptArg,AddTomru=defaultNamedOptArg,Local=defaultNamedOptArg
41188,CorruptLoad=defaultNamedOptArg):
> 41189 ret = self._oleobj_.InvokeTypes(1923,LCID,1,(13,0),((8,1),(12,17),17)),Filename
41190,WriteResPassword
41191,Notify
com_error: (-2147352567,'Ocurrió una excepción.',(0,'Microsoft Excel','Lo sentimos,no hemos enconTrado test_file.xlsx. ¿Puede ser que lo haya movido,eliminado o le hayas cambiado el nombre?','xlmain11.chm',-2146827284),None)
西班牙语的最后一行说“抱歉,我们找不到 test_file.xlsx。难道我已经移动、删除或重命名了它?”
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)