This article mainly introduces Python's method of batch reading table information in word, which can be used to read word documents. it has some reference value, for more information, see the following example. Share it with you for your reference. The details are as follows:
The organization has collected a lot of word-format surveys. leaders need to collect information in the forms. I put all the surveys in one file and wrote a python applet to print the required information.
# Coding: utf-8import osimport win32comfrom win32com. client import Dispatch, constantsfrom docx import Documentdef parse_doc (f): "reads doc, returns name and industry" doc = w. documents. open (FileName = f) t = doc. tables [0] # select information name = t based on the chart in the file. rows [0]. cells [1]. range. text situation = t. rows [0]. cells [5]. range. text people = t. rows [1]. cells [1]. range. text title = t. rows [1]. cells [3]. range. text print name, situation, people, title doc. close () def parse_docx (f): "" read docx, return name and industry "d = Document (f) t = d. tables [0] name = t. cell (0, 1 ). text situation = t. cell (0, 8 ). text people = t. cell (1, 2 ). text title = t. cell (1, 8 ). text print name, situation, people, titleif _ name _ = "_ main _": w = win32com. client. dispatch ('Word. application ') # traverse the file PATH = "H: \ work \ aaa" # windows file PATH doc_files = OS. listdir (PATH) for doc in doc_files: if OS. path. splitext (doc) [1] = '.docx ': try: parse_docx (PATH +' \ '+ doc) failed t Exception as e: print e elif OS. path. splitext (doc) [1] = '.doc ': try: parse_doc (PATH +' \ '+ doc) failed t Exception as e: print e
I hope this article will help you with Python programming.