File size: 1,133 Bytes
118ef23
d4886ae
e38c499
118ef23
d4886ae
 
 
118ef23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c109a3
 
118ef23
 
d4886ae
 
b98b7df
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from flask import Flask, request, render_template
from docx import Document
from io import StringIO
from lxml import etree

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/convert', methods=['POST'])
def convert():
    file = request.files['file']
    document = Document(file)
    html = document_to_html(document)
    return html

def document_to_html(document):
    """
    将Word文档转换为HTML格式字符串。
    :param document: Word文档对象
    :return: HTML格式字符串
    """
    # 将Word文档转换为XML
    xml = document_to_xml(document)

    # 使用lxml库将XML转换为HTML
    xslt = etree.parse('docx2html.xslt')
    transform = etree.XSLT(xslt)
    html = str(transform(xml))

    return html

def document_to_xml(document):
    """
    将Word文档转换为XML格式字符串。
    :param document: Word文档对象
    :return: XML格式字符串
    """
    xml = StringIO()
    with open(xml, 'wb') as f:
    document.save(f)
    xml.seek(0)
    return xml.read()

if __name__ == '__main__':
    app.run(port=7860,host='0.0.0.0')