Spaces:
Sleeping
Sleeping
File size: 1,993 Bytes
d4886ae |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
from flask import Flask, request, send_file
from docx import Document
from pdf2docx import Converter
import os
if not os.path.exists('uploads'):
os.makedirs('uploads')
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def convert_docx_to_pdf():
if request.method == 'POST':
file = request.files['file']
# 保存上传的docx文件
docx_path = 'uploads/input.docx'
file.save(docx_path)
# 将docx转换为pdf
pdf_path = 'uploads/output.pdf'
cv = Converter(docx_path)
cv.convert(pdf_path)
cv.close()
return send_file(pdf_path, as_attachment=True)
return '''
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>将.docx文件转换为.pdf</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f2f2f2;
}
h1 {
text-align: center;
color: #333;
}
form {
max-width: 400px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
input[type="file"] {
display: block;
margin-bottom: 15px;
}
input[type="submit"] {
width: 100%;
padding: 10px;
font-size: 16px;
color: #fff;
background-color: #007bff;
border: none;
border-radius: 5px;
cursor: pointer;
}
@media only screen and (max-width: 600px) {
form {
width: 90%;
}
}
.powered-by {
text-align: center;
font-size: 12px;
color: #999;
padding: 10px;
}
.powered-by a {
color: #666;
text-decoration: none;
}
.powered-by a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>在线将.docx文件转换为.pdf</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="转换">
</form>
</body>
<div class="powered-by">
Powered by <a href="/">森林处理器</a>
</div>
</html>
'''
if __name__ == '__main__':
app.run(port=7860,host='0.0.0.0')
|