如何使用python打开受密码保护的excel文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19450837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:46:19  来源:igfitidea点击:

How to open a password protected excel file using python?

pythonexcelfile-iopasswordsprotected

提问by Schack

I looked at the previous threads regarding this topic, but they have not helped solve the problem.

我查看了有关此主题的先前线程,但它们并没有帮助解决问题。

I'm trying to open a password protected file in excel without any user interaction. I searched online, and found this code which uses win32com.client When I run this, I still get the prompt to enter the password...

我试图在没有任何用户交互的情况下在 excel 中打开受密码保护的文件。网上查了一下,发现这段代码使用了win32com.client 运行这个,还是提示输入密码...

from xlrd import *
import win32com.client
import csv
import sys

xlApp = win32com.client.Dispatch("Excel.Application")
print "Excel library version:", xlApp.Version
filename,password = r"\HRA\Myfile.xlsx", 'caa team'
xlwb = xlApp.Workbooks.Open(filename, Password=password)

采纳答案by Bjoern Stiel

I don't think that named parameters work in this case. So you'd have to do something like:

我认为命名参数在这种情况下不起作用。因此,您必须执行以下操作:

xlwb = xlApp.Workbooks.Open(filename, False, True, None, password)

xlwb = xlApp.Workbooks.Open(文件名,假,真,无,密码)

See http://msdn.microsoft.com/en-us/library/office/ff194819.aspxfor details on the Workbooks.Open method.

有关Workbooks.Open 方法的详细信息,请参阅http://msdn.microsoft.com/en-us/library/office/ff194819.aspx

回答by Raj

Openpyxl Package works if you are using linux system. You can use secure the file by setting up a password and open the file using the same password.

如果您使用的是 linux 系统,则 Openpyxl 包有效。您可以通过设置密码来保护文件并使用相同的密码打开文件。

For more info: https://www.quora.com/How-do-I-open-read-password-protected-xls-or-xlsx-Excel-file-using-python-in-Linux

更多信息:https: //www.quora.com/How-do-I-open-read-password-protected-xls-or-xlsx-Excel-file-using-python-in-Linux

回答by GpandaM

If your file size is small, you can probably save that as ".csv". and then read

如果您的文件很小,您可以将其另存为“.csv”。然后阅读

It worked for me :)

它对我有用:)