Spaces:
Sleeping
Sleeping
Upload docxtopdf.py
Browse files- docxtopdf.py +106 -0
docxtopdf.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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')
|