smf2010 commited on
Commit
118ef23
1 Parent(s): d64481d

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +40 -100
main.py CHANGED
@@ -1,106 +1,46 @@
1
- from flask import Flask, request, send_file
2
  from docx import Document
3
- from pdf2docx import Converter
4
- import os
5
-
6
- if not os.path.exists('uploads'):
7
- os.makedirs('uploads')
8
 
9
  app = Flask(__name__)
10
 
11
- @app.route('/', methods=['GET', 'POST'])
12
- def convert_docx_to_pdf():
13
- if request.method == 'POST':
14
- file = request.files['file']
15
-
16
- # 保存上传的docx文件
17
- docx_path = 'uploads/input.docx'
18
- file.save(docx_path)
19
-
20
- # 将docx转换为pdf
21
- pdf_path = 'uploads/output.pdf'
22
- cv = Converter(docx_path)
23
- cv.convert(pdf_path)
24
- cv.close()
25
-
26
- return send_file(pdf_path, as_attachment=True)
27
-
28
- return '''
29
- <!doctype html>
30
- <html>
31
- <head>
32
- <meta charset="UTF-8">
33
- <title>将.docx文件转换为.pdf</title>
34
- <style>
35
- body {
36
- font-family: Arial, sans-serif;
37
- margin: 0;
38
- padding: 20px;
39
- background-color: #f2f2f2;
40
- }
41
-
42
- h1 {
43
- text-align: center;
44
- color: #333;
45
- }
46
-
47
- form {
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(port=7860,host='0.0.0.0')
 
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)