初用python-docx

事情的起因是這樣的,女朋友的公司在做一個(gè)數(shù)字化管道的工作,需要在大量的word中提取想要的數(shù)據(jù),手動(dòng)輸入實(shí)在太麻煩,就看到這個(gè)python-docx的庫(kù),肥腸的高效,腳本發(fā)出來(lái)給大家。

!/usr/bin/python

-- coding: UTF-8 --

author = 'Administrator'

讀取docx中的文本代碼示例

import docx
from docx import Document #導(dǎo)入庫(kù)
import re
import glob
import os

第一種表格

path = "E:\test\test.docx" #文件路徑

document = Document(path) #讀入文件

tables = document.tables #獲取文件中的表格集

for i in range(0,len(tables)):

table = tables[0]#獲取文件中的第一個(gè)表格

print(table.cell(2,1).text)

print(table.cell(4,1).text)

print(table.cell(4,6).text)

datestr = table.cell(8,0).text.replace(" ","")

mat = re.search(r"(\d{4}年\d{1,2}月\d{1,2}日)",datestr)

print(mat.group(0))

for i in range(0,len(table.rows)):#從表格第二行開始循環(huán)讀取表格數(shù)據(jù)

result = table.cell(i,0).text + "" +table.cell(i,1).text+table.cell(i,2).text + table.cell(i,3).text

#cell(i,0)表示第(i+1)行第1列數(shù)據(jù),以此類推

print(result)

第二種表格

f = open("result.txt",'w',encoding='UTF-8')

path = "test02.docx" #文件路徑

document = Document(path) #讀入文件

tables = document.tables #獲取文件中的表格集

table = tables[0]#獲取文件中的第一個(gè)表格

print(len(tables))

for table in tables:

for i in range(0,len(table.rows)):#從表格第二行開始循環(huán)讀取表格數(shù)據(jù)

if table.cell(i,0).text.isdigit():

mark = table.cell(i,10).text

num = mark

split = ""

m = '0'

if mark.endswith('m'):

mat = re.search(r"([\d,\.]*m)",mark)

m =mat.group(0)

num = mark.replace(m,"")

split = num[len(num)-1]

num=num[0:len(num)-1]

result = table.cell(i,0).text + "\t" +table.cell(i,1).text+"\t"+table.cell(i,7).text +"\t"+ table.cell(i,6).text+"\t"+ table.cell(i,2).text.replace("X:","")+"\t"+ table.cell(i,3).text.replace("Y:","")+"\t"+ num+"\t"+ split+"\t"+ m.replace('m','')

print(result)

f.write(result+"\n")

f.close()

第三種表格

os.chdir("E:\test\input\815\QAB管道焊縫檢測(cè)報(bào)告\")

paths = []
paths.extend(glob.glob('*.%s' % 'docx'))
fs = open("射線.txt",'w',encoding='UTF-8')
fc = open("超聲波.txt",'w',encoding='UTF-8')
for path in paths:
#path = "1.管道焊縫檢測(cè)報(bào)告(QAB171-1~QAB189-1).docx" #文件路徑
document = Document(path) #讀入文件
tables = document.tables #獲取文件中的表格集
#table = tables[0]#獲取文件中的第一個(gè)表格
#print(len(tables))
for table in tables:
title = (table.cell(0,2).text)
if("無(wú)損檢測(cè)返修通知單" not in title and "超聲波檢測(cè)報(bào)告" not in title and "附頁(yè)" in title):
a2 = (table.cell(1,2).text)
a3 = (table.cell(2,2).text.replace(" ",""))
try:
a6 = table.cell(24,0).text.split("\n")[0].replace("評(píng)定人員:","").replace("評(píng)定人員:","").split("級(jí) 別:")[0]
a7 = table.cell(24,11).text.split("\n")[0].replace("監(jiān)理(簽字):","")
except IndexError as e:
a6 = table.cell(23,0).text.split("\n")[0].replace("評(píng)定人員:","").replace("評(píng)定人員:","").split("級(jí) 別:")[0]
a7 = table.cell(23,11).text.split("\n")[0].replace("監(jiān)理(簽字):","")

        for i in range(0,len(table.rows)):#從表格第二行開始循環(huán)讀取表格數(shù)據(jù)
            if table.cell(i,0).text.isdigit():
                result = table.cell(i,1).text+"\t"+a2+"\t"+a3+"\t"+table.cell(i,10).text+"\t"+table.cell(i,12).text+"\t"+a6+"\t"+a7
                result = result.replace('\n','')
                print(path+"==="+result)
                fs.write(result+"\n")
    elif("無(wú)損檢測(cè)返修通知單" not in title and "射線檢測(cè)報(bào)告" not in title and "附頁(yè)" in title):
        a2 = (table.cell(1,2).text)
        a3 = (table.cell(2,2).text.replace(" ",""))
        a6 = table.cell(25,0).text.split("\n")[0].replace("檢測(cè)人員:","").replace("檢測(cè)人員:","").split("級(jí)  別:")[0]
        a7 = table.cell(25,11).text.split("\n")[0].replace("監(jiān)理(簽字):","")
        for i in range(0,len(table.rows)):#從表格第二行開始循環(huán)讀取表格數(shù)據(jù)
            if table.cell(i,0).text.isdigit():
                result = table.cell(i,1).text+"\t"+a2+"\t"+a3+"\t"+table.cell(i,6).text+" "+table.cell(i,7).text+" "+table.cell(i,9).text+"\t"+table.cell(i,12).text+"\t"+a6+"\t"+a7
                result = result.replace('\n','')
                print(path+"==="+result)
                fc.write(result+"\n")

fs.close()
fc.close()

第四種表格

f = open("result.txt",'w',encoding='UTF-8')

path = "E:\test\input\815\1.docx" #文件路徑

document = Document(path) #讀入文件

tables = document.tables #獲取文件中的表格集

table = tables[0]#獲取文件中的第一個(gè)表格

print(len(tables))

for table in tables:

a = table.cell(1,4).text

ass = a.split("-")

if len(ass) < 3:

a =ass[0]

else:

print(a)

b = table.cell(8,2).text

bs = b.replace('\n\n\n',"\n").split('\n')

b1 = bs[0].replace("監(jiān)理代表:","")

b2 = bs[1].replace(" ","").replace("年","-").replace("月","-").replace("日","")

result = a.replace("\n","")+"\t"+b2.replace("\n","")+"\t"+b1.replace("\n","")

#print(result)

f.write(result+"\n")

f.close()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容