fix export file docx
All checks were successful
Setup Native Action / native (3.12.7) (pull_request) Has been skipped
Setup Native Action / docker (3.12.7) (pull_request) Has been skipped

This commit is contained in:
XuanHuyen 2025-01-18 01:06:32 +07:00
parent 03078ee41f
commit 21495b24f6
2 changed files with 29 additions and 17 deletions

View File

@ -17,6 +17,7 @@ from docx.table import _Cell
from docx.shared import Inches, Pt from docx.shared import Inches, Pt
import html2text import html2text
import platform import platform
import tempfile
class DocxTemplate(models.Model): class DocxTemplate(models.Model):
@ -82,10 +83,9 @@ class DocxTemplate(models.Model):
def get_image(self, paragraph, value): def get_image(self, paragraph, value):
image = Image.open(value).convert("RGBA") 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 = paragraph.add_run()
#run.add_picture('/tmp/out.png') run.add_picture(os.path.join(tempfile.gettempdir(), 'out.png'), width=Inches(1),height=Inches(1))
run.add_picture('C:/tmp/out.png',width=Inches(1),height=Inches(1))
def make_docx_pdf_report(self): def make_docx_pdf_report(self):
active_ids = self._context.get('active_ids') active_ids = self._context.get('active_ids')
@ -102,12 +102,15 @@ class DocxTemplate(models.Model):
if docx_template_id: if docx_template_id:
# existing file read # existing file read
val = base64.b64decode(docx_template_id.docx_file) 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.write(val)
autfile.close() autfile.close()
for rec in records: for rec in records:
doc = docx.Document("C:/tmp/output.docx") doc = docx.Document(file_path)
for doc_style in doc.styles: for doc_style in doc.styles:
if 'font' in dir(doc_style): if 'font' in dir(doc_style):
if docx_template_id.font_name: if docx_template_id.font_name:
@ -216,7 +219,8 @@ class DocxTemplate(models.Model):
if str(type(res_value)) == "<class '_io.BytesIO'>": if str(type(res_value)) == "<class '_io.BytesIO'>":
#add img in o2m table #add img in o2m table
image = Image.open(res_value).convert("RGBA") 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] paragraph = row_cells[i].paragraphs[0]
run = paragraph.add_run() run = paragraph.add_run()
#set size of img cell in o2m table #set size of img cell in o2m table
@ -225,7 +229,7 @@ class DocxTemplate(models.Model):
row_cells[i].height = Inches(2) row_cells[i].height = Inches(2)
### ###
#add img in cell #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: else:
if res_value: if res_value:
row_cells[i].text = str(res_value)#add value of new line's cell 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)) == "<class '_io.BytesIO'>": if str(type(res_value)) == "<class '_io.BytesIO'>":
#add img after o2m table #add img after o2m table
image = Image.open(res_value).convert("RGBA") 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] paragraph = row_cells[i].paragraphs[0]
run = paragraph.add_run() run = paragraph.add_run()
#set size of img cell in o2m table #set size of img cell in o2m table
@ -257,7 +262,7 @@ class DocxTemplate(models.Model):
cell.height = Inches(2) cell.height = Inches(2)
### ###
#add img in cell #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: else:
if res_value: if res_value:
row_cells[i].text = str(res_value) row_cells[i].text = str(res_value)
@ -274,18 +279,21 @@ class DocxTemplate(models.Model):
my_dict[str(k)] = value my_dict[str(k)] = value
docx_replace(doc, **my_dict)#default package docx_replace(doc, **my_dict)#default package
#self.docx_replace(doc, my_dict)#use custom b'cz of binary field #self.docx_replace(doc, my_dict)#use custom b'cz of binary field
doc.save("C:/tmp/final.docx") output_path = os.path.join(tempfile.gettempdir(), 'final.docx')
input_file = '/tmp/final.docx' os.makedirs(os.path.dirname(output_path), exist_ok=True)
output_file = '/tmp/final.pdf' 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( #os.system(
# f"soffice --headless --convert-to pdf --outdir {os.path.dirname(os.path.realpath(output_file))} {input_file}") # save as pdf # f"soffice --headless --convert-to pdf --outdir {os.path.dirname(os.path.realpath(output_file))} {input_file}") # save as pdf
os_name = platform.system() os_name = platform.system()
try: try:
if os_name == 'Linux': 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: 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: except:
_logger.error("\n\n An Exception occured \n\n") _logger.error("\n\n An Exception occured \n\n")
if docx_template_id.skip_wizard: if docx_template_id.skip_wizard:

View File

@ -3,6 +3,8 @@
from odoo import api, fields, models, _ from odoo import api, fields, models, _
import base64 import base64
import os
import tempfile
class DocxTemplateWiz(models.TransientModel): class DocxTemplateWiz(models.TransientModel):
@ -23,12 +25,14 @@ class DocxTemplateWiz(models.TransientModel):
filename = False filename = False
mime_type = False mime_type = False
if self.print_type == 'pdf': if self.print_type == 'pdf':
file_path_pdf = os.path.join(tempfile.gettempdir(), 'final.pdf')
file = open("C:/tmp/final.pdf", "rb") file = open(file_path_pdf, "rb")
filename = str(docx_template_id.name)+'.pdf' filename = str(docx_template_id.name)+'.pdf'
mime_type = 'application/pdf' mime_type = 'application/pdf'
if self.print_type == 'docx': 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' filename = str(docx_template_id.name)+'.docx'
mime_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' mime_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
if file and filename: if file and filename: