How do I extract information from a python file? 3 minutes to understand Python text parsing and extraction

Source: Internet
Author: User
The unit collects many questionnaires in Word format, and the leader needs to collect the form's Information, I put all the questionnaires in a file, wrote a Python applet to print out the required information, this small program can be from Analyze information and extract information in python text

#coding: Utf-8 import osimport win32comfrom win32com.client import Dispatch, Constantsfrom docx import Document def parse_d OC (f): "" reads Doc, returns the name and Industry "" "Doc = W.documents.open (FileName = f) T = Doc. TABLES[0] # Select information based on the chart in the file name = T.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): "" reads docx, returns 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. Text title = T.cell (1,8). Text print name, situation, people,title if __name__ = = "__main__": w = win32com.client.Dispatch (' Word.Application ') # Traverse file path = "H:\work\\aaa" # Windows file Paths Doc_fi Les = 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 e elif os.path.splitext (doc) [1] = = '. Doc ': Try:parse_doc (path+ ' \ \ ' +doc) except Exception as E:print E 

Download and install win32com

From win32com import client as WC Word = WC. Dispatch (' Word.Application ') doc = Word. Documents.Open (' C:/test ') doc. SaveAs (' C:/test.text ', 2) doc. Close () word. Quit ()

The text document produced in this way cannot be read in Python in normal R mode, so that Python can be read in R, it should be written as
Doc. SaveAs (' C:/test ', 4)
Note: When the system executes, the file suffix txt is automatically generated (although no suffix is specified).
Below the XP system, you should,

Open (R ' C:\text ', ' r ') wdformatdocument = 0 WdFormatDocument97 = 0 Wdformatdocumentdefault = wdFormatDOSText = 4 WDFORMATD Ostextlinebreaks = 5 Wdformatencodedtext = 7 Wdformatfilteredhtml = Ten Wdformatflatxml = wdformatflatxmlmacroenabled =  Wdformatflatxmltemplate = wdformatflatxmltemplatemacroenabled = wdformathtml = 8 Wdformatpdf = WdFormatRTF = 6 wdFormatTemplate = 1  wdFormatTemplate97 = 1 Wdformattext = 2 Wdformattextlinebreaks = 3 Wdformatunicodetext = 7  w dformatwebarchive = 9 Wdformatxml = one  wdformatxmldocument = wdformatxmldocumentmacroenabled = WdFormatXMLTempl ate =  wdformatxmltemplatemacroenabled = Wdformatxps = 18

The literal meaning should be able to correspond to the corresponding file format, if you are Office 2003 may not support so many formats. Word file to HTML has two formats optional wdformathtml, wdformatfilteredhtml (corresponding to the number 8, 10), the difference is if the wdformathtml format, OLE objects, such as formulas inside Word files, will be stored in the WMF format, and the formula images will be stored in GIF format using wdformatfilteredhtml, and the HTML generated with wdformatfilteredhtml can be seen visually Obviously much cleaner than wdformathtml.

Related Article

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.