File size: 1,021 Bytes
428a607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import io


from flask import redirect, Blueprint, request, url_for, flash, send_file
from docx import Document

file = Blueprint('file', __name__, template_folder='templates', url_prefix='/dashboard/file')

@file.route('/ekspor', methods=['POST'])
def ekspor():
    nama = request.form['nama']
    nim = request.form['nim']
    file_type = request.form['file_type']

    data = request.form.to_dict()

    doc = Document(f'resources/{file_type}.docx')

    try:
        for p in doc.paragraphs:
            for key, value in data.items():
                if f'{{{{{key}}}}}' in p.text:
                    p.text = p.text.replace(f'{{{{{key}}}}}', value)

        buffer = io.BytesIO()
        doc.save(buffer)
        buffer.seek(0)

        return send_file(buffer, as_attachment=True, download_name=f'{file_type}_{nama}_{nim}.docx', mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document')

    except Exception as e:
        flash(('Ekspor Berkas Gagal', 'Berkas gagal diekspor'), 'error')