daniel muchiri
daniel muchiri

Reputation: 11

How can I insert DOCX type content created from a Ckeditor by HtmlToDocx, in to a table's cell created by Python DOCX?

Function that converts CKeditor HTML content into DOCX using HtmlToDocx

def html_to_docs(html):
        new_parser.table_style = 'Table Grid'
        return new_parser.add_html_to_document(html, document)

Table created by Python DOCX table

# reportdetails is a django model

table = document.add_table(rows=reportdetails.count()-1, cols=5, style='Table Grid')
    table.allow_autofit = True 
    table.columns[0].width = Cm(2)
    table.columns[1].width = Cm(16)
    table.columns[2].width = Cm(2)
    table.columns[3].width = Cm(2)
    table.columns[4].width = Cm(2)
    hdr_cells = table.rows[0].cells
    hdr_cells[0].text = 'Research Area'
    hdr_cells[1].text = 'Findings'
    hdr_cells[2].text = 'Impact'
    hdr_cells[3].text = 'Recommendations'
    hdr_cells[4].text = 'Response'
    for reportdetail in reportdetails:
                
        row_cells = table.add_row().cells
               
        row_cells[0].text = reportdetail.scope.scope_name
        finding = row_cells[1].add_paragraph()
        finding.add_run(str(html_to_docs(reportdetail.findings)))#inserting CKeditor content
        row_cells[2].text = reportdetail.impact
        row_cells[3].text = reportdetail.recommendations
        row_cells[4].text = reportdetail.response

Output I am getting on word document genetated:

Output I am getting on word document genetated

No trees planted should be the first finding and in the findings cell of row Climate, the table with those alphabets and the line after it should be in the second findings.

I have no idea why the findings cells contains None

Upvotes: 1

Views: 313

Answers (0)

Related Questions