Using Python to read table information in word in batches

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.