Spaces:
Sleeping
Sleeping
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__) | |
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') | |