Se rendre au contenu
Menu
Cette question a été signalée
1 Répondre
1163 Vues

Hello everyone, sorry for the inconvenience, is it possible to import a password-protected Excel file into Odoo?

Avatar
Ignorer
Meilleure réponse

No it is not possible but you can use online password removal after that you can import that file
or 
Open the password-protected Excel file in Excel or another spreadsheet application. and copy to another one

Avatar
Ignorer
Auteur

ah ok thanks but my problem is that I can receive more than 100 files per day and if I want to copy to another file it can slow down the work

in that case just write script where you can encrypt password and update it
just like
def Remove_password_xlsx(filename, pw_str):
xcl = win32com.client.Dispatch("Excel.Application")
wb = xcl.Workbooks.Open(filename, False, False, None, pw_str)
xcl.DisplayAlerts = False
wb.SaveAs(filename, None, '', '')
xcl.Quit()

after that you can do your desired outcome

Auteur

here is my code help me adapt it

from io import BytesIO

from odoo import api, fields, models, _
from odoo.exceptions import UserError
import openpyxl
import base64

#
#
# class ExcelImportWizard(models.TransientModel):
# _name = 'excel.import.wizard'
#
# excel_file = fields.Binary(string='Excel File', required=True)
# password = fields.Char(string='Password', default='IPACRCI', required=True)
#
#
# def import_excel_data(self):
# # Vérifier si un fichier Excel est téléchargé
# if not self.excel_file:
# raise UserError(_("Please upload an Excel file."))
#
# # Ouvrir le fichier Excel avec le mot de passe fourni
# excel_data = None
# try:
# excel_workbook = openpyxl.load_workbook(filename=False, data=self.excel_file, read_only=True,
# keep_links=False, pa

class ExcelImportWizard(models.TransientModel):
_name = 'excel.import.wizard'

excel_file = fields.Binary(string='Excel File', required=True)
password = fields.Char(string='Password', default='IPACRCI')

def import_excel_data(self):

if not self.excel_file:
raise UserError(_("Please upload an Excel file."))

try:

file_content = base64.b64decode(self.excel_file)
excel_workbook = openpyxl.load_workbook(filename=BytesIO(file_content), read_only=True, keep_links=False,
password=self.password)
excel_sheet = excel_workbook.active
excel_data = excel_sheet.iter_rows(values_only=True)
except Exception as e:
raise UserError(("Failed to open Excel file: %s" % e))

for row in excel_data:
self.env['excel.imported.data'].create({
'field1': row[0],
'field2': row[1],

})

raise UserError(_("Excel data imported successfully."))

class ExcelImportedData(models.Model):
_name = 'excel.imported.data'
_description = 'Excel Imported Data'

field1 = fields.Char(string='Field 1')
field2 = fields.Char(string='Field 2')

import pandas as pd
from openpyxl import load_workbook

# Open the password-protected file
password = 'your_password'
file_path = 'path/to/password_protected_file.xlsx'
wb = load_workbook(filename=file_path, read_only=False, keep_vba=True)

add this
wb.security.workbookPassword = password
wb.save(file_path)

# Now read the unlocked file
df = pd.read_excel(file_path)

# Process the data and import it into Odoo

Auteur

Really thank you for your help and sorry for the inconvenience, but how to do to download the file since I have a file download interface where I introduce in my old code? THANK YOU AGAIN FOR THE TIME YOU GIVE ME

step 1:-
pip install msoffcrypto
step 2 :-
from odoo import models, fields, _
from odoo.exceptions import UserError
import base64
from io import BytesIO
import openpyxl
import msoffcrypto

class ExcelImportWizard(models.TransientModel):
_name = 'excel.import.wizard'

excel_file = fields.Binary(string='Excel File', required=True)
password = fields.Char(string='Password', default='IPACRCI')

def import_excel_data(self):
if not self.excel_file:
raise UserError(_("Please upload an Excel file."))

try:
file_content = base64.b64decode(self.excel_file)
decrypted_file = BytesIO()
office_file = msoffcrypto.OfficeFile(BytesIO(file_content))
office_file.load_key(password=self.password)
office_file.decrypt(decrypted_file)
decrypted_file.seek(0)

excel_workbook = openpyxl.load_workbook(filename=decrypted_file, read_only=True, keep_links=False)
excel_sheet = excel_workbook.active
excel_data = excel_sheet.iter_rows(values_only=True)
except Exception as e:
raise UserError(_("Failed to open Excel file: %s" % e))

for row in excel_data:
self.env['excel.imported.data'].create({
'field1': row[0],
'field2': row[1],
})

raise UserError(_("Excel data imported successfully."))

class ExcelImportedData(models.Model):
_name = 'excel.imported.data'
_description = 'Excel Imported Data'

field1 = fields.Char(string='Field 1')
field2 = fields.Char(string='Field 2')

Thanks
Feel free to connect :
Email: bhushanwagh292@gmail.com
LinkedIn: https://in.linkedin.com/in/bhushan-wagh-09a161160
Happy to help :)

Publications associées Réponses Vues Activité
1
juin 24
1912
1
juin 24
1195
2
juin 24
3866
1
juin 24
1706
2
juin 24
1893