def get_word_from_html(self, html_string):
     document = Document()
     new_parser = HtmlToDocx()
     new_parser.add_html_to_document(html_string, document)
     doc_io = io.BytesIO()
     document.save(doc_io)
     doc_io.seek(0)
     return doc_io
Exemplo n.º 2
0
def render_to_docx(context, template, doc_type, opis=''):
    tpl = get_template(template)
    html_text = tpl.render(context)
    document = Document()
    style = document.styles['Normal']
    font = style.font
    font.name = 'Dijakritika'
    new_parser = HtmlToDocx()
    new_parser.add_html_to_document(html_text, document)
    temp_file = tempfile.TemporaryFile()
    document.save(temp_file)
    novi_dokument = add_file_to_django(doc_type, opis, temp_file, 'docx')
    return novi_dokument.rendered_file.name
Exemplo n.º 3
0
def generate_word_application(application, request):
    # figure out the filename
    target_dir = os.path.join(settings.BASE_DIR, 'media', 'temp')
    target_file = "temp_export.docx"
    target_file_path = os.path.join(target_dir, target_file)
    target_url = os.path.join(settings.MEDIA_ROOT, 'temp', target_file)
    document = Document()
    t = loader.get_template('res/application_print/main_word.html')
    context = get_application_context(application, request)
    rendered = t.render(context)
    new_parser = HtmlToDocx()
    new_parser.add_html_to_document(rendered, document)

    document.save(target_file_path)
    return target_url
Exemplo n.º 4
0
def download(request):
    """Download a transliteration as document"""

    # Get the download type
    qd = request.POST
    if 'downloadtype' in qd and 'transliteration' in qd:
        # Get the download type and the data itself
        dtype = qd['downloadtype']
        transliteration = qd['transliteration']

        if dtype == "docx":
            dext = ".docx"
            sContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
            # Transform HTML to DOC
            document = Document()
            new_parser = HtmlToDocx()
            new_parser.add_html_to_document(transliteration, document)

            buffer = io.BytesIO()  # Start memory stream
            document.save(buffer)  # Save to memory stream
            buffer.seek(0)  # Rewind the stream

        # Determine a file name
        sBase = self.object.name
        sFileName = "trans_{}{}".format(sBase, dext)

        if dtype == "docx":
            response = StreamingHttpResponse(streaming_content=buffer,
                                             content_type=sContentType)
            response['Content-Encoding'] = 'UTF-8'
            response[
                'Content-Disposition'] = 'attachment; filename="{}"'.format(
                    sFileName)
        else:
            response = HttpResponse(ddata, content_type=sContentType)
            response[
                'Content-Disposition'] = 'attachment; filename="{}"'.format(
                    sFileName)
    else:
        # Return something else
        response = None

    # Return the response we have
    return response
Exemplo n.º 5
0
def create_document(csv_data):

    try:
        html_file = open("temp.html", "w")
        html_file.write("<!DOCTYPE html>\n")
        html_file.write("<html>\n")
        html_file.write("<head>\n")
        html_file.write("<meta charset='utf-8'>\n")
        html_file.write("</head>\n")
        html_file.write("<body>\n")
        row_data = {}

        for key, val in inclusion_list.items():
            html_file.write(f"<h1> {key} </h1>\n")
            for a in val:
                for i in csv_data:
                    if i["Issue key"] == a:
                        row_data["Issue key"] = check_string(i["Issue key"])
                        row_data["Summary"] = check_string(i["Summary"])
                        row_data["Description"] = check_string(
                            i["Description"])

                html_file.write(
                    f"<h3> {row_data['Issue key']} : {row_data['Summary']} </h3>\n"
                )
                line = row_data['Description']
                line = OrderedBulletList(line).parse_string()
                line = UnOrderedBulletList(line).parse_string()
                line = replace_image(line)
                line = parse_bold(line)
                line = parse_heading(line)
                html_file.write(f"{line}\n")

        html_file.write("</body>\n")
        html_file.write("</html>\n")

        # Convert HTML file to docx
        new_parser = HtmlToDocx()
        new_parser.parse_html_file("temp.html", "gaps_items_html_converted")

    except Exception as e:
        logging.error(str(e))
Exemplo n.º 6
0
def genDoc(request):
    # print(request.POST.get('ids')[0])
    final = ""
    parser = HtmlToDocx()

    for doc in Document.objects.all():
        final += doc.content + "<br/>\n"

    # final += "<br/>\n<hr/>\n<h2>References:</h2>"

    final += "<br/><h2>References</h2><br/>\n"

    for doc in Document.objects.all():
        final += doc.reference + "<br/>"

    result = parser.parse_html_string(final)
    print(parser.parse_html_string(final))
    response = HttpResponse(
        content_type=
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    )
    response['Content-Disposition'] = 'attachment; filename=download.docx'
    result.save(response)
    return response
Exemplo n.º 7
0
"""
将HTML转为word,注意把源码的编码设为utf-8
"""
from htmldocx import HtmlToDocx

new_parser = HtmlToDocx()
new_parser.parse_html_file('1.html', '1.docx')
Exemplo n.º 8
0
from htmldocx import HtmlToDocx

new_parser = HtmlToDocx()
file = '/home/akshay/MindCodes/writingartist/writing_artist/user_files/1/2021-02-10_10_37_21_211100.html'

new_parser.from_file(file)
Exemplo n.º 9
0
def insert_text(document, text):
    my_parser = HtmlToDocx()
    md = MarkdownIt()
    md.enable('table')
    my_parser.add_html_to_document(md.render(text), document)
Exemplo n.º 10
0
 def export_docx(path: str, document: Document) -> str:
     html = Exporter.render_html(document.content, document.title)
     html_parser = HtmlToDocx()
     docx = html_parser.parse_html_string(html)
     docx.save(path)
     return path