From 21495b24f6ae059296c85a5da132a3d8c0dddf02 Mon Sep 17 00:00:00 2001 From: XuanHuyen Date: Sat, 18 Jan 2025 01:06:32 +0700 Subject: [PATCH] fix export file docx --- .../models/docx_template.py | 36 +++++++++++-------- .../wizard/docx_template_wiz.py | 10 ++++-- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/extra-addons/dynamic_docx_pdf_reports_omax/models/docx_template.py b/extra-addons/dynamic_docx_pdf_reports_omax/models/docx_template.py index 742bc7fd1..8f54e386c 100644 --- a/extra-addons/dynamic_docx_pdf_reports_omax/models/docx_template.py +++ b/extra-addons/dynamic_docx_pdf_reports_omax/models/docx_template.py @@ -17,6 +17,7 @@ from docx.table import _Cell from docx.shared import Inches, Pt import html2text import platform +import tempfile class DocxTemplate(models.Model): @@ -82,10 +83,9 @@ class DocxTemplate(models.Model): def get_image(self, paragraph, value): image = Image.open(value).convert("RGBA") - image.save('C:/tmp/out.png') + image.save(os.path.join(tempfile.gettempdir(), 'out.png')) run = paragraph.add_run() - #run.add_picture('/tmp/out.png') - run.add_picture('C:/tmp/out.png',width=Inches(1),height=Inches(1)) + run.add_picture(os.path.join(tempfile.gettempdir(), 'out.png'), width=Inches(1),height=Inches(1)) def make_docx_pdf_report(self): active_ids = self._context.get('active_ids') @@ -102,12 +102,15 @@ class DocxTemplate(models.Model): if docx_template_id: # existing file read val = base64.b64decode(docx_template_id.docx_file) - with open('C:/tmp/output.docx', 'wb') as autfile: + # get temp directory + file_path = os.path.join(tempfile.gettempdir(), 'output.docx') + os.makedirs(os.path.dirname(file_path), exist_ok=True) # create directory if it doesn't exist + with open(file_path, 'wb') as autfile: autfile.write(val) autfile.close() for rec in records: - doc = docx.Document("C:/tmp/output.docx") + doc = docx.Document(file_path) for doc_style in doc.styles: if 'font' in dir(doc_style): if docx_template_id.font_name: @@ -216,7 +219,8 @@ class DocxTemplate(models.Model): if str(type(res_value)) == "": #add img in o2m table image = Image.open(res_value).convert("RGBA") - image.save('C:/tmp/out1.png') + file_image = os.path.join(tempfile.gettempdir(), 'out1.png') + image.save(file_image) paragraph = row_cells[i].paragraphs[0] run = paragraph.add_run() #set size of img cell in o2m table @@ -225,7 +229,7 @@ class DocxTemplate(models.Model): row_cells[i].height = Inches(2) ### #add img in cell - run.add_picture('C:/tmp/out1.png',width=Inches(1),height=Inches(1)) + run.add_picture(file_image,width=Inches(1),height=Inches(1)) else: if res_value: row_cells[i].text = str(res_value)#add value of new line's cell @@ -248,7 +252,8 @@ class DocxTemplate(models.Model): if str(type(res_value)) == "": #add img after o2m table image = Image.open(res_value).convert("RGBA") - image.save('C:/tmp/out.png') + file_image = os.path.join(tempfile.gettempdir(), 'out1.png') + image.save(file_image) paragraph = row_cells[i].paragraphs[0] run = paragraph.add_run() #set size of img cell in o2m table @@ -257,7 +262,7 @@ class DocxTemplate(models.Model): cell.height = Inches(2) ### #add img in cell - run.add_picture('C:/tmp/out.png',width=Inches(1.5),height=Inches(1.5)) + run.add_picture(file_image,width=Inches(1.5),height=Inches(1.5)) else: if res_value: row_cells[i].text = str(res_value) @@ -274,18 +279,21 @@ class DocxTemplate(models.Model): my_dict[str(k)] = value docx_replace(doc, **my_dict)#default package #self.docx_replace(doc, my_dict)#use custom b'cz of binary field - doc.save("C:/tmp/final.docx") - input_file = '/tmp/final.docx' - output_file = '/tmp/final.pdf' + output_path = os.path.join(tempfile.gettempdir(), 'final.docx') + os.makedirs(os.path.dirname(output_path), exist_ok=True) + doc.save(output_path) + input_file = os.path.join(tempfile.gettempdir(), 'final.docx') + output_file = os.path.join(tempfile.gettempdir(), 'final.pdf') + os.makedirs(os.path.dirname(output_file), exist_ok=True) #create directory if it doesn't exist #os.system( # f"soffice --headless --convert-to pdf --outdir {os.path.dirname(os.path.realpath(output_file))} {input_file}") # save as pdf os_name = platform.system() try: if os_name == 'Linux': - os.system(f"libreoffice --headless --convert-to pdf /tmp/final.docx --outdir /tmp") + os.system(f"libreoffice --headless --convert-to pdf {input_file} --outdir /tmp") else: - os.system(f"soffice --headless --convert-to pdf:writer_pdf_Export C:/tmp/final.docx --outdir /tmp") + os.system(f"soffice --headless --convert-to pdf:writer_pdf_Export {output_file} --outdir /tmp") except: _logger.error("\n\n An Exception occured \n\n") if docx_template_id.skip_wizard: diff --git a/extra-addons/dynamic_docx_pdf_reports_omax/wizard/docx_template_wiz.py b/extra-addons/dynamic_docx_pdf_reports_omax/wizard/docx_template_wiz.py index 4d4fe3319..df1c05000 100644 --- a/extra-addons/dynamic_docx_pdf_reports_omax/wizard/docx_template_wiz.py +++ b/extra-addons/dynamic_docx_pdf_reports_omax/wizard/docx_template_wiz.py @@ -3,6 +3,8 @@ from odoo import api, fields, models, _ import base64 +import os +import tempfile class DocxTemplateWiz(models.TransientModel): @@ -23,12 +25,14 @@ class DocxTemplateWiz(models.TransientModel): filename = False mime_type = False if self.print_type == 'pdf': - - file = open("C:/tmp/final.pdf", "rb") + file_path_pdf = os.path.join(tempfile.gettempdir(), 'final.pdf') + file = open(file_path_pdf, "rb") filename = str(docx_template_id.name)+'.pdf' mime_type = 'application/pdf' if self.print_type == 'docx': - file = open("C:/tmp/final.docx", "rb") + file_path_docx = os.path.join(tempfile.gettempdir(), 'final.docx') + os.makedirs(os.path.dirname(file_path_docx), exist_ok=True) + file = open(file_path_docx, "rb") filename = str(docx_template_id.name)+'.docx' mime_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' if file and filename: