python如何获取word文件中某个关键字之后的表格
最好是全部都读取到程序中,在程序中进行判断。
本文实例讲述了Python实现批量读取word中表格信息的方法。
分享给大家供大家参考。
具体如下:单位收集了很多word格式的调查表,领导需要收集表单里的信息,我就把所有调查表放一个文件里,写了个python小程序把所需的信息打印出来#coding:utf-8import osimport win32comfrom win32com.client import Dispatch, constantsfrom docx import Documentdef parse_doc(f):"""读取doc,返回姓名和行业"""doc = w.Documents.Open( FileName = f )t = doc.Tables[0] # 根据文件中的图表选择信息name = t.Rows[0].Cells[1].Range.Textsituation = t.Rows[0].Cells[5].Range.Textpeople = t.Rows[1].Cells[1].Range.Texttitle = t.Rows[1].Cells[3].Range.Textprint name, situation, people,titledoc.Close()def parse_docx(f):"""读取docx,返回姓名和行业"""d = Document(f)t = d.tables[0]name = t.cell(0,1).textsituation = t.cell(0,8).textpeople = t.cell(1,2).texttitle = t.cell(1,8).textprint name, situation, people,titleif __name__ == "__main__":w = win32com.client.Dispatch('Word.Application')# 遍历文件PATH = "H:\work\\aaa" # windows文件路径doc_files = os.listdir(PATH)for doc in doc_files:if os.path.splitext(doc)[1] == '.docx':try:parse_docx(PATH+'\\'+doc)except Exception as e:print eelif os.path.splitext(doc)[1] == '.doc':try:parse_doc(PATH+'\\'+doc)except Exception as e:print e希望本文所述对大家的Python程序设计有所帮助。
pyt