I want to protect an Excel with password which is available in S3 bucket and save it back to s3, I tried with openpyxl and xlsxwriter, it is generating xlsx file, but it opens without asking for password
import openpyxl
import shutil
def password_protect_excel(filename, password):
# Load the workbook from the mount location
workbook = openpyxl.load_workbook(filename)
# Placeholder for password protection
print(f"Attempting to set a password on the workbook: {password}")
# Save the workbook to a local temporary path first
temp_local_path = "/tmp/Report_temp.xlsx"
workbook.save(temp_local_path)
return temp_local_path
if __name__ == "__main__":
# Define the mount location paths
input_file = "/dbfs/mnt/file_mount_report/Report.xlsx"
output_file = "/dbfs/mnt/file_mount_report/Report_protected_new.xlsx"
# Password placeholder
password = "your_strong_password"
# Step 1: Process the Excel file
temp_local_path = password_protect_excel(input_file, password)
# Step 2: Move the saved file to the mount location
shutil.copy(temp_local_path, output_file)
print(f"Workbook saved at {output_file}")
New contributor
MOHAMMED SALMAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.