I am trying to protect an excel sheet with Openyxl in Python, I have tried in different ways but without success. The idea is to create a data entry so that the user can enter data and then verify it in excel without the possibility of modifying it. Only supervisors can modify the information, so they must have the password.
import os
import openpyxl
import tkinter
from tkinter import ttk
def enter_data():
nombre = first_name_entry.get()
apellido = last_name_entry.get()
print("Nombre: ", nombre, "Apellido: ", apellido)
filepath= "C:/Users/franc/Documents/Python/Prueba con excel/Viaticos.xlsx"
if not os.path.exists(filepath):
workbook = openpyxl.Workbook()
sheet = workbook.active
heading = ["Nombre", "Apellido"]
sheet.append(heading)
workbook.save(filepath)
workbook = openpyxl.load_workbook(filepath)
sheet = workbook.active
sheet.append([nombre, apellido])
workbook.save(filepath)
window = tkinter.Tk()
window.title('Viaticos Neutics')
frame = tkinter.Frame(window)
frame.pack()
user_info_frame = tkinter.LabelFrame(frame, text="Informacion Personal")
user_info_frame.grid(row= 0, column=0, padx=20, pady=20)
#Name and last name
first_name_label = tkinter.Label(user_info_frame, text="Nombre")
first_name_label.grid(row=0, column=0)
last_name_label = tkinter.Label(user_info_frame, text = "Apellido")
last_name_label.grid(row=0, column=1)
#Name and lastname
first_name_entry = tkinter.Entry(user_info_frame)
first_name_entry.grid(row=1, column=0)
last_name_entry = tkinter.Entry(user_info_frame)
last_name_entry.grid(row=1, column=1)
window.mainloop()
I would like to make something like this:
import os
import openpyxl
import tkinter
from tkinter import ttk
def enter_data():
nombre = first_name_entry.get()
apellido = last_name_entry.get()
print("Nombre: ", nombre, "Apellido: ", apellido)
filepath= "C:/Users/franc/Documents/Python/Prueba con excel/Viaticos.xlsx"
#set a password
password = "123"
#unprotect the sheet
sheet.unprotect.password
workbook = openpyxl.load_workbook(filepath)
sheet = workbook.active
#protect the sheet before save
sheet.protect.password
sheet.append([nombre, apellido])
workbook.save(filepath)
window = tkinter.Tk()
window.title('Viaticos Neutics')
frame = tkinter.Frame(window)
frame.pack()
user_info_frame = tkinter.LabelFrame(frame, text="Informacion Personal")
user_info_frame.grid(row= 0, column=0, padx=20, pady=20)
#Name and last name
first_name_label = tkinter.Label(user_info_frame, text="Nombre")
first_name_label.grid(row=0, column=0)
last_name_label = tkinter.Label(user_info_frame, text = "Apellido")
last_name_label.grid(row=0, column=1)
#Name and lastname
first_name_entry = tkinter.Entry(user_info_frame)
first_name_entry.grid(row=1, column=0)
last_name_entry = tkinter.Entry(user_info_frame)
last_name_entry.grid(row=1, column=1)
window.mainloop()
CodePudding user response:
I have solved it with the help document of Openpyxl. https://openpyxl.readthedocs.io/en/stable/protection.html At first I didn't understand it but then I understood how it works First you have to import the configuration "from openpyxl.workbook.protection import WorkbookProtection"
then create a password for the file
ws = workbook.active
ws.protection.password = '123'
and it would be ready. The file is checked out only when the dataenter performs a modification