简体   繁体   English

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

[英]How to open a password protected excel file using python?

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.我试图在没有任何用户交互的情况下在 excel 中打开受密码保护的文件。 I searched online, and found this code which uses win32com.client When I run this, I still get the prompt to enter the password...网上查了一下,发现这段代码使用了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)

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)

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

I recently discovered a Python library that makes this task simple.我最近发现了一个 Python 库,它使这个任务变得简单。

It does not require Excel to be installed and, because it's pure Python, it's cross-platform too!它不需要安装 Excel,而且因为它是纯 Python,所以它也是跨平台的!

msoffcrypto-tool supports password-protected (encrypted) Microsoft Office documents, including the older XLS binary file format. msoffcrypto-tool支持受密码保护(加密)的 Microsoft Office 文档,包括旧的 XLS 二进制文件格式。

  • Install msoffcrypto-tool:安装 msoffcrypto 工具:

     pip install msoffcrypto-tool
  • You could create an unencrypted version of the workbook from the command line:您可以从命令行创建工作簿的未加密版本:

     msoffcrypto-tool Myfile.xlsx Myfile-decrypted.xlsx -p "caa team"
  • Or, you could use msoffcrypto-tool as a library.或者,您可以使用 msoffcrypto-tool 作为库。 While you could write an unencrypted version to disk like above, you may prefer to create an decrypted in-memory file and pass this to your Python Excel library ( openpyxl , xlrd , etc.).虽然您可以像上面一样将未加密的版本写入磁盘,但您可能更喜欢创建一个解密的内存文件并将其传递给您的 Python Excel 库( openpyxlxlrd等)。

     import io import msoffcrypto import openpyxl decrypted_workbook = io.BytesIO() with open('Myfile.xlsx', 'rb') as file: office_file = msoffcrypto.OfficeFile(file) office_file.load_key(password='caa team') office_file.decrypt(decrypted_workbook) # `filename` can also be a file-like object. workbook = openpyxl.load_workbook(filename=decrypted_workbook)

If your file size is small, you can probably save that as ".csv".如果您的文件很小,您可以将其另存为“.csv”。 and then read然后阅读

It worked for me :)它对我有用:)

Openpyxl Package works if you are using linux system.如果您使用的是 linux 系统,则 Openpyxl 包有效。 You can use secure the file by setting up a password and open the file using the same password.您可以通过设置密码来保护文件并使用相同的密码打开文件。

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

Thank you so much for the great answers on this topic.非常感谢您对这个主题的精彩回答。 Trying to collate all of it.试图整理所有这些。 My requirement was to open a bunch of password protected excel files ( all had same password ) so that I could do some more processing on those.我的要求是打开一堆受密码保护的 excel 文件(都具有相同的密码),以便我可以对这些文件进行更多处理。 Please find the code below.请在下面找到代码。

import pandas as pd
import os

from xlrd import *
import win32com.client as w3c
import csv
import sys
from tempfile import NamedTemporaryFile

    df_list=[]
#    print(len(files))
    for f in files:
#    print(f)
    if('.xlsx' in f):


        xlwb = xlapp.Workbooks.Open('C:\\users\\files\\'+f, False, True, None, 'TDE@123')

        temp_f = NamedTemporaryFile(delete=False, suffix='.csv')  
        temp_f.close()
        os.unlink(temp_f.name)  

        xlwb.SaveAs(Filename=temp_f.name, FileFormat=xlCSVWindows) 
        df = pd.read_csv(temp_f.name,encoding='Latin-1')  # Read that CSV from Pandas
        df.to_excel('C:\\users\\files\\password_removed\\'+f)
        

      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM