如何在Python中对Excel文件进行密码保护,但允许只读访问?

huangapple go评论92阅读模式
英文:

How to password protect an excel file but allow read only in python?

问题

You can password-protect an Excel file in Python using the openpyxl library. Here's the modified code to save your Excel file with a write password:

import pandas as pd
from openpyxl import Workbook
from openpyxl.workbook.protection import WorkbookProtection

# Your DataFrame
df4 = ...

# Create a new Excel Workbook
wb = Workbook()

# Add the DataFrame to the Workbook
ws = wb.active
for row in df4.iterrows():
    ws.append(row[1].tolist())

# Set a write password to protect the Workbook
wb.security = WorkbookProtection(workbookPassword="your_password")

# Save the Workbook
wb.save("asd.xlsx")

Replace "your_password" with the desired write password for your Excel file.

Complete code after modification:

import pandas as pd
from openpyxl import Workbook
from openpyxl.workbook.protection import WorkbookProtection

# Your DataFrame
df4 = ...

# Create a new Excel Workbook
wb = Workbook()

# Add the DataFrame to the Workbook
ws = wb.active
for row in df4.iterrows():
    ws.append(row[1].tolist())

# Set a write password to protect the Workbook
wb.security = WorkbookProtection(workbookPassword="your_password")

# Save the Workbook
wb.save("asd.xlsx")

This code will create an Excel file ("asd.xlsx") and protect it with a write password.

英文:

I'm working on automatization in python and and I already created a read only excel file but is it possible to make it password protect to write?

thank all help 如何在Python中对Excel文件进行密码保护,但允许只读访问?

I've made it this far

with pd.ExcelWriter("asd.xlsx", engine="xlsxwriter") as excel_writer:
    df4.to_excel(excel_writer, index=False, sheet_name="Sheet1")
    excel_writer.book.read_only_recommended()

答案1

得分: 0

欢迎来到Stack Overflow。有很多方法可以完成这个任务。这个答案做了一些假设...您只能为整个工作簿、特定的Excel工作表等添加密码保护。您的问题有一些不清楚的地方。

例如,您是否希望仅使用Pandas库来实现密码保护?如果是的话,那是不可能的(参考来源),或者是否使用其他库来修改工作簿或工作表就足够了。我假设是后者。

一些潜在的解决方案是使用Aspose.Cells库为工作簿添加密码保护。

ASPOSE保护类型:

> - ALL - 用户无法修改任何内容。
> - CONTENTS - 用户无法输入数据。
> - OBJECTS - 用户无法修改绘图对象。
> - SCENARIOS - 用户无法修改保存的场景。
> - STRUCTURE - 用户无法修改保存的结构。
> - WINDOWS - 用户无法修改保存的窗口。
> - NONE - 无保护。

#库
import jpype
import asposecells
import pandas as pd

jpype.startJVM()
from asposecells.api import Workbook, ProtectionType

#加载Excel文件
workbook = Workbook("asd.xlsx")

#添加密码保护
workbook.protect(ProtectionType.STRUCTURE, "password")

#保存受保护的Excel文件
workbook.save("protected-asd.xlsx")

#继续使用受保护文件进行Pandas数据框操作...
with pd.ExcelWriter("protected-asd.xlsx", engine="xlsxwriter") as excel_writer:
df4.to_excel(excel_writer, index=False, sheet_name="Sheet1")
英文:

Welcome to Stack Overflow. There is a myriad of ways you could accomplish this task. This answer has made several assumptions...you could only add password protection to an entire workbook, a specific excel worksheet, etc. There are a few things about your question that was not clear.

For example, would you like to accomplish the password protection using the Pandas library only? If so, then that is not possible (reference source) or if using another library to modify the workbook or sheet would suffice. I assumed the latter.

Some potential solutions are to add the password protection to your workbook using the Aspose.Cells library.

ASPOSE Protection Types:

> - ALL - User cannot modify anything.
> - CONTENTS - User cannot enter data.
> - OBJECTS - User cannot modify drawing objects.
> - SCENARIOS - User cannot modify saved scenarios.
> - STRUCTURE - User cannot modify saved structure.
> - WINDOWS - User cannot modify saved windows.
> - NONE - No protection.

#Libraries
import jpype
import asposecells
import pandas as pd

jpype.startJVM()
from asposecells.api import Workbook, ProtectionType

#Load Excel File
workbook = Workbook("asd.xlsx")
    
#Add password protection
workbook.protect(ProtectionType.STRUCTURE, "password")

#Save the protected excel file
workbook.save("protected-asd.xlsx")

#Continue with Pandas dataframe here using the protected file...
with pd.ExcelWriter("protected-asd.xlsx", engine="xlsxwriter") as excel_writer:
df4.to_excel(excel_writer, index=False, sheet_name="Sheet1")

答案2

得分: 0

需要使用Python按密码打开它,就像这样:

import io
import openpyxl
import msoffcrypto

decryptedWorkbook = io.BytesIO()

with open('yourFile.xlsx', 'rb') as file:
    excelFile = msoffcrypto.OfficeFile(file)
    excelFile.load_key(password='your password')
    excelFile.decrypt(decryptedWorkbook)

myWorkbook = openpyxl.load_workbook(filename=decryptedWorkbook)
英文:

you need to open it by password using python like:

import io
import openpyxl
import msoffcrypto

decryptedWorkbook = io.BytesIO()

with open('yourFile.xlsx', 'rb') as file:
    excelFile = msoffcrypto.OfficeFile(file)
    excelFile.load_key(password='your password')
    excelFile.decrypt(decryptedWorkbook)

myWorkbook = openpyxl.load_workbook(filename=decryptedWorkbook)

答案3

得分: 0

你可以使用Aspose.Cells for Python通过Java轻松完成此任务。请参考以下示例代码,该代码将使Excel文件受写保护,并设置密码供您参考。
例如。
示例代码:

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook

# 加载MS Excel文件。
workbook = Workbook("asd.xlsx")
    

# 使用密码写保护工作簿。
workbook.getSettings().getWriteProtection().setPassword("1234");

# 在写保护工作簿时指定作者。
workbook.getSettings().getWriteProtection().setAuthor("testauthor1");


# 保存受写保护的Excel文件。
workbook.save("out_protected-asd.xlsx")

当您手动在MS Excel中打开输出文件时,将提示您指定写保护密码。如果您想更改/更新文件中的内容,您需要提供正确的密码。否则,您可以以“只读”方式打开文件,仅供阅读目的。

您还可以在专用的论坛中发布您的查询或评论。

附注:我在Aspose的支持开发/传道者部门工作。

英文:

You may accomplish the task using Aspose.Cells for Python via Java easily. See the following sample code that will make the Excel file write-protected with password for your reference.
e.g.
Sample code:

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook

# Load the MS Excel file.
workbook = Workbook("asd.xlsx")
    

# Write protect workbook with password.
workbook.getSettings().getWriteProtection().setPassword("1234");

# Specify author while write protecting workbook.
workbook.getSettings().getWriteProtection().setAuthor("testauthor1");


# Save the write-protected Excel file.
workbook.save("out_protected-asd.xlsx")

When you open the output file into MS Excel manually, you will be prompted to specify the write-protected password. If you want to change/update the contents in the file, you will give the correct password. Otherwise, you can open the file as "Read only" for read-only purpose.

You may also post your queries or comments in the dedicated forums.

PS. I am working as Support developer/ Evangelist at Aspose.

huangapple
  • 本文由 发表于 2023年8月10日 22:19:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定