前言:
最近有需求處理docx文件,并講內(nèi)容顯示到頁面,對world進(jìn)行在線的閱讀,這樣我這里就使用flask+Document對docx文件進(jìn)行處理并顯示,下面直接上代碼:
Document處理:
首先下載Document的庫文件,先直接安裝最新版的python-docx,如果不行則換成1.1.0版本:
pip install python-docx
pip install python-docx==1.1.0
處理docx代碼如下:
def ReadVADocx(ProjectName,DocxName):
docxfilepath = vaReportDir + "\\" + ProjectName + "\\" + DocxName
paragraphs = ReadDocx(docxfilepath)
return paragraphs
def ReadDocx(docxfilepath):
doc = Document(docxfilepath)
paragraphs = list()
pattern = re.compile('rId\d+')
for graph in doc.paragraphs:
level = graph.style.name.split(' ')[-1]
if level == "Normal":
level = None
elif level == "Preformatted":
level = None
paragraph = {
'text': graph.text,
'level': level,
'images': ""
}
paragraphs.append(paragraph)
for run in graph.runs:
if run.text == '':
contentID = pattern.search(run.element.xml)
if contentID:
contentID = contentID.group(0)
try:
contentType = doc.part.related_parts[contentID].content_type
except KeyError as e:
print(e)
continue
if not contentType.startswith('image'):
continue
imgData = doc.part.related_parts[contentID].blob
image_base64 = base64.b64encode(imgData).decode('utf-8')
paragraph = {
'text': run.text,
'level': run.style.name.split(' ')[-1] if run.style.name.startswith('Heading') else None,
'images': image_base64
}
paragraphs.append(paragraph)
上述代碼會(huì)對docx文件進(jìn)行遍歷,并將對應(yīng)的內(nèi)容和等級放入數(shù)組中
下面是調(diào)用代碼:
@app.route('/ViewVADocx', methods=['GET'])
def ViewVADocx():
try:
DocxName = request.args.get('docx')
ProjectName = request.args.get('name')
paragraphs = engine.ReadVADocx(ProjectName,DocxName)
return render_template("viewdocx.html", n_getname=ProjectName, n_user=user,paragraphs=paragraphs)
except Exception as e:
return render_template('error-500.html')
html編寫:?
然后就是需要講對應(yīng)的內(nèi)容在頁面進(jìn)行展示,下面列出html代碼:
{% extends "mould.html" %}
{% block head %}
{% endblock %}
{% block body %}
<!--body wrapper start-->
<div class="wrapper">
<div class="floating-box" id="floatingBox">↑回到頂部↑</div>
<!--Start Page Title-->
<div class="page-title-box">
<h4 class="page-title">{{ n_getname }}:掃描節(jié)點(diǎn)線</h4>
<div class="clearfix"></div>
</div>
<!--End Page Title-->
<!--Start row-->
<div class="row">
<div class="col-md-12">
<div class="white-box">
<h2 style="font-weight: bold;">快速導(dǎo)航:</h2>
{% for paragraph in paragraphs %}
{% if paragraph.level == "1" %}
<p>
<a href="#Section{{ loop.index0 }}" class="hover-link" style="font-weight: bold;">{{ paragraph.text }}</a>
{% elif paragraph.level == "2" %}
<p style="text-indent: 25px;">
<a href="#Section{{ loop.index0 }}" class="hover-link2" style="font-weight: bold;">{{ paragraph.text }}	</a>
</p>
{% endif %}
{% endfor %}
</div>
{% for paragraph in paragraphs %}
{% if paragraph.level %}
{% if paragraph.level == "Title" %}
<!-- <h2 align="center">{{ paragraph.text }}</h2>-->
{% elif paragraph.level == "1" %}
</div>
<div class="white-box">
<h{{ paragraph.level }} id="Section{{ loop.index0 }}" style="font-weight: bold;">{{ paragraph.text }}</h{{ paragraph.level }}>
{% else %}
<h{{ paragraph.level }} id="Section{{ loop.index0 }}">{{ paragraph.text }}</h{{ paragraph.level }}>
{% endif %}
{% else %}
{% if paragraph.images %}
<p><img src="data:image/png;base64,{{ paragraph.images }}" alt="Image"></p>
{% else %}
<p style="color: black;">{{ paragraph.text }}</p>
{% endif %}
{% endif %}
{% endfor %}
</div>
</div>
</div>
{% endblock %}
{% block list %}
<style>
.hover-link {
font-size: 20px;
}
.hover-link:hover {
color: red;
font-size: 30px;
}
.hover-link2 {
font-size: 15px;
}
.hover-link2:hover {
color: red;
font-size: 20px;
}
</style>
<style>
/* CSS 樣式,用于定義懸浮框的外觀 */
.floating-box {
position: fixed;
bottom: 20px;
right: 20px;
width: 80px;
height: 50px;
background-color: #ff9900;
color: #fff;
text-align: center;
line-height: 50px;
cursor: pointer;
}
</style>
<script>
// JavaScript 代碼
var floatingBox = document.getElementById('floatingBox');
// 點(diǎn)擊事件監(jiān)聽器
floatingBox.addEventListener('click', function() {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
</script>
{% endblock %}
其中添加了樣式和回到頂部等小功能,方便瀏覽,最后的使用效果如下:
?
后記:
代碼只做了docx文件的內(nèi)容展示,包括文字和圖片,并對等級進(jìn)行了劃分,沒有對docx的修改功能,感興趣的可以自己研究下?
?文章來源地址http://www.zghlxwxcb.cn/news/detail-775373.html文章來源:http://www.zghlxwxcb.cn/news/detail-775373.html
?
到了這里,關(guān)于python3處理docx并flask顯示的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!