Djangoueditor (rich text) text extraction and export word Realization

When storage is rich text with HTML code, so you need to extract the contents of the rich text to plain text in the rich text export word document

Text extraction There are two ways recommend the second

method one:

Background Import Package

from django.template.defaultfilters import striptags # extract rich text text
We need to extract at call
text = striptags (obj_wen.ued) # obj_wen.ued rich text stored in the database name

                          The original text

                   Plain text

By contrast, the method may extract the plain text but poor English filter
Method Two
Djangoueditor method using official documents
Obtain the plain text contents of the front page and in the rich text editor into the background by Form
function getPlainTxt() {
        text = UE.getEditor ( 'id_content') getPlainTxt ();. // Get Rich Text Editor plain text
        //document.getElementById("wenben").innerHTML=text;
        $ ( "# Wenben") attr ( "value", text);. // assign value passed in the background
    }
id_content a front end view in FIG ueditor id F12 by opening the page by:
 

Extraction results

This method basically perfect solution English plain text extraction, which need to pay attention when you call in need of extracting text editor after the start, may be submitted in written form

Export to word download and implement all the same

First introduced python word packet

from docx import Document 
Then build documentation
    document = Document () #docx function
    document.add_heading(obj_wen.biaoti, 0)  #写入标题
    text = (obj_wen.textfield)  #数据库纯文本
    document.styles['Normal'].font.name = u'黑体'   #字体格式黑体
    p = document.add_paragraph()    
    p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER  #居中
    run = p.add_run(text)     #写入文字
导入包实现下载文件名实现中文
from django.utils.http import urlquote   #中文文件名
    response = HttpResponse(content_type='application/octet-stream')   #缓存申明 octet-stream为文件类型
    response['Content-Disposition']='attachment;filename="%s.doc"'% urlquote(obj_wen.biaoti)    #下载说明 文件名 
    document.save(response) #文件保存入缓存
    return response
前端调用即可实现下载

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

希望对你有所帮助!

Guess you like

Origin www.cnblogs.com/ddb1-1/p/11783729.html