Load OpenXML content in Django admin TextField keeping styles

301 views Asked by At

I'm trying to use Django 1.8 admin interface + RedactorJS (via django-wysiwyg-redactor) to load a doc file into Django model's field, enabled with the RedactorEditor plugin. On other words: allow the user to load and modify a simple DOC file using a textarea from the Django admin.

So far, I've been able to read the doc (using oodocx), transform it to OpenXML string using lxml, and store that string in model's field of type Text.

Besides, I can use the RedactorEditor widget to load that field, and have a toolbar TinyMCE style, to modify the content of the loaded DOC file.

The problem is I lose all the styles. And as a picture worths a thousand words, here it comes

My original word file

enter image description here

How the field looks like in the Django admin field

enter image description here

And this is the content of the text field, as Unicode OpenXML string:

u'<w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"><w:p><w:pPr><w:pStyle w:val="Normal"/><w:jc w:val="center"/><w:rPr/></w:pPr><w:r><w:rPr/><w:t>Text text text text text text text</w:t></w:r></w:p><w:p><w:pPr><w:pStyle w:val="Normal"/><w:rPr/></w:pPr><w:r><w:rPr/></w:r></w:p><w:p><w:pPr><w:pStyle w:val="Normal"/><w:rPr><w:b/><w:b/><w:bCs/><w:color w:val="800000"/></w:rPr></w:pPr><w:r><w:rPr><w:b/><w:bCs/><w:color w:val="800000"/></w:rPr><w:t>RED TEXT RED TEXT RED TEXT RED TEXT</w:t></w:r></w:p><w:p><w:pPr><w:pStyle w:val="Normal"/><w:rPr/></w:pPr><w:r><w:rPr/></w:r></w:p><w:p><w:pPr><w:pStyle w:val="Normal"/><w:rPr/></w:pPr><w:r><w:rPr/></w:r></w:p><w:p><w:pPr><w:pStyle w:val="Normal"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="1"/></w:numPr><w:rPr/></w:pPr><w:r><w:rPr/><w:t>POINT 1</w:t></w:r></w:p><w:p><w:pPr><w:pStyle w:val="Normal"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="1"/></w:numPr><w:rPr/></w:pPr><w:r><w:rPr/><w:t>POINT 2</w:t></w:r></w:p><w:p><w:pPr><w:pStyle w:val="Normal"/><w:numPr><w:ilvl w:val="0"/><w:numId w:val="1"/></w:numPr><w:rPr/></w:pPr><w:r><w:rPr/><w:t>POINT 3</w:t></w:r></w:p><w:sectPr><w:type w:val="nextPage"/><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:left="1134" w:right="1134" w:header="0" w:top="1134" w:footer="0" w:bottom="1134" w:gutter="0"/><w:pgNumType w:fmt="decimal"/><w:formProt w:val="false"/><w:textDirection w:val="lrTb"/></w:sectPr></w:body>'

So, how could I make the redactorjs Django widget to understand the OpenXML format and apply the styles? Should I use another different Javascript editor? Try to write my own redactorJS plugin? A different approach, maybe?

1

There are 1 answers

0
Jorge Arévalo On

Solved: transformed the OpenXML content of the DOC file to HTML using python-docx. After that, just load the HTML in the redactorjs editor