Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -1,106 +1,46 @@
|
|
1 |
-
from flask import Flask, request,
|
2 |
from docx import Document
|
3 |
-
from
|
4 |
-
import os
|
5 |
-
|
6 |
-
if not os.path.exists('uploads'):
|
7 |
-
os.makedirs('uploads')
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
-
@app.route('/'
|
12 |
-
def
|
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 |
-
max-width: 400px;
|
49 |
-
margin: 0 auto;
|
50 |
-
background-color: #fff;
|
51 |
-
padding: 20px;
|
52 |
-
border-radius: 5px;
|
53 |
-
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
|
54 |
-
}
|
55 |
-
|
56 |
-
input[type="file"] {
|
57 |
-
display: block;
|
58 |
-
margin-bottom: 15px;
|
59 |
-
}
|
60 |
-
|
61 |
-
input[type="submit"] {
|
62 |
-
width: 100%;
|
63 |
-
padding: 10px;
|
64 |
-
font-size: 16px;
|
65 |
-
color: #fff;
|
66 |
-
background-color: #007bff;
|
67 |
-
border: none;
|
68 |
-
border-radius: 5px;
|
69 |
-
cursor: pointer;
|
70 |
-
}
|
71 |
-
|
72 |
-
@media only screen and (max-width: 600px) {
|
73 |
-
form {
|
74 |
-
width: 90%;
|
75 |
-
}
|
76 |
-
}
|
77 |
-
.powered-by {
|
78 |
-
text-align: center;
|
79 |
-
font-size: 12px;
|
80 |
-
color: #999;
|
81 |
-
padding: 10px;
|
82 |
-
}
|
83 |
-
.powered-by a {
|
84 |
-
color: #666;
|
85 |
-
text-decoration: none;
|
86 |
-
}
|
87 |
-
.powered-by a:hover {
|
88 |
-
text-decoration: underline;
|
89 |
-
}
|
90 |
-
</style>
|
91 |
-
</head>
|
92 |
-
<body>
|
93 |
-
<h1>在线将.docx文件转换为.pdf</h1>
|
94 |
-
<form method="post" enctype="multipart/form-data">
|
95 |
-
<input type="file" name="file">
|
96 |
-
<input type="submit" value="转换">
|
97 |
-
</form>
|
98 |
-
</body>
|
99 |
-
<div class="powered-by">
|
100 |
-
Powered by <a href="/">森林处理器</a>
|
101 |
-
</div>
|
102 |
-
</html>
|
103 |
-
'''
|
104 |
|
105 |
if __name__ == '__main__':
|
106 |
-
app.run(
|
|
|
1 |
+
from flask import Flask, request, render_template
|
2 |
from docx import Document
|
3 |
+
from lxml import etree
|
|
|
|
|
|
|
|
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
+
@app.route('/')
|
8 |
+
def index():
|
9 |
+
return render_template('index.html')
|
10 |
+
|
11 |
+
@app.route('/convert', methods=['POST'])
|
12 |
+
def convert():
|
13 |
+
file = request.files['file']
|
14 |
+
document = Document(file)
|
15 |
+
html = document_to_html(document)
|
16 |
+
return html
|
17 |
+
|
18 |
+
def document_to_html(document):
|
19 |
+
"""
|
20 |
+
将Word文档转换为HTML格式字符串。
|
21 |
+
:param document: Word文档对象
|
22 |
+
:return: HTML格式字符串
|
23 |
+
"""
|
24 |
+
# 将Word文档转换为XML
|
25 |
+
xml = document_to_xml(document)
|
26 |
+
|
27 |
+
# 使用lxml库将XML转换为HTML
|
28 |
+
xslt = etree.parse('docx2html.xslt')
|
29 |
+
transform = etree.XSLT(xslt)
|
30 |
+
html = str(transform(xml))
|
31 |
+
|
32 |
+
return html
|
33 |
+
|
34 |
+
def document_to_xml(document):
|
35 |
+
"""
|
36 |
+
将Word文档转换为XML格式字符串。
|
37 |
+
:param document: Word文档对象
|
38 |
+
:return: XML格式字符串
|
39 |
+
"""
|
40 |
+
xml = StringIO()
|
41 |
+
document.save(xml)
|
42 |
+
xml.seek(0)
|
43 |
+
return xml.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
if __name__ == '__main__':
|
46 |
+
app.run(debug=True)
|