環境:PyCharm python3.7
需要下載python-docx、docx(打開Settings->Project Interpreter->+搜索docx、python-docx install即可)
來源:https://blog.csdn.net/xtfge0915/article/details/83479922
#獲取文章全部內容
doc=docx.Document('D:\\Users\\Administrator\\PycharmProjects\\BigData\\Detail\\a.docx')
一級標題
for p in doc.paragraphs:
if p.style.name=='Heading 1':
print(p.text)
#二級標題
for p in doc.paragraphs:
if p.style.name=='Heading 2':
print(p.text)
#所有標題
import re
for p in doc.paragraphs:
if re.match("^Heading \d+$",p.style.name):
print(p.text)
#所有內容
for p in doc.paragraphs:
if p.style.name=='Normal':
print(p.text)
#從前面可以看出,如果知道不同內容的style.name,那么要讀這些內容是極其方便的,這些style.name可以通過:
#print(p.style.name)得到
for p in doc.paragraphs:
if p.style.name=='級別3:黑體 13磅 20行距 段落前后20 左對齊':
print(p.text)
#輸出對應內容