事情的起因是这样的,女朋友的公司在做一个数字化管道的工作,需要在大量的word中提取想要的数据,手动输入实在太麻烦,就看到这个python-docx的库,肥肠的高效,脚本发出来给大家。
!/usr/bin/python
-- coding: UTF-8 --
author = 'Administrator'
读取docx中的文本代码示例
import docx
from docx import Document #导入库
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]#获取文件中的第一个表格
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)):#从表格第二行开始循环读取表格数据
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列数据,以此类推
print(result)
第二种表格
f = open("result.txt",'w',encoding='UTF-8')
path = "test02.docx" #文件路径
document = Document(path) #读入文件
tables = document.tables #获取文件中的表格集
table = tables[0]#获取文件中的第一个表格
print(len(tables))
for table in tables:
for i in range(0,len(table.rows)):#从表格第二行开始循环读取表格数据
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管道焊缝检测报告\")
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.管道焊缝检测报告(QAB171-1~QAB189-1).docx" #文件路径
document = Document(path) #读入文件
tables = document.tables #获取文件中的表格集
#table = tables[0]#获取文件中的第一个表格
#print(len(tables))
for table in tables:
title = (table.cell(0,2).text)
if("无损检测返修通知单" not in title and "超声波检测报告" not in title and "附页" 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("评定人员:","").replace("评定人员:","").split("级 别:")[0]
a7 = table.cell(24,11).text.split("\n")[0].replace("监理(签字):","")
except IndexError as e:
a6 = table.cell(23,0).text.split("\n")[0].replace("评定人员:","").replace("评定人员:","").split("级 别:")[0]
a7 = table.cell(23,11).text.split("\n")[0].replace("监理(签字):","")
for i in range(0,len(table.rows)):#从表格第二行开始循环读取表格数据
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("无损检测返修通知单" not in title and "射线检测报告" not in title and "附页" 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("检测人员:","").replace("检测人员:","").split("级 别:")[0]
a7 = table.cell(25,11).text.split("\n")[0].replace("监理(签字):","")
for i in range(0,len(table.rows)):#从表格第二行开始循环读取表格数据
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()
网友评论