How to use python to read word documents

Source: Internet
Author: User
This article mainly introduces how to read a word document from python. The example analyzes the skills related to how to operate a Word document from Python based on win32com. if you need it, refer to the example below to describe how to read a word document from python. Share it with you for your reference. The details are as follows:

First download and install win32com

from win32com import client as wcword = wc.Dispatch('Word.Application')doc = word.Documents.Open('c:/test')doc.SaveAs('c:/test.text', 2)doc.Close()word.Quit()

Text documents generated in this way cannot be read in the normal r mode in python. to allow python to read in the r mode, it should be written

doc.SaveAs('c:/test', 4)

Note: After the system completes execution, the file suffix txt is automatically generated (although no suffix is specified ).
Under the XP system,

open(r'c:\text','r')wdFormatDocument = 0wdFormatDocument97 = 0wdFormatDocumentDefault = 16wdFormatDOSText = 4wdFormatDOSTextLineBreaks = 5wdFormatEncodedText = 7wdFormatFilteredHTML = 10wdFormatFlatXML = 19wdFormatFlatXMLMacroEnabled = 20wdFormatFlatXMLTemplate = 21wdFormatFlatXMLTemplateMacroEnabled = 22wdFormatHTML = 8wdFormatPDF = 17wdFormatRTF = 6wdFormatTemplate = 1wdFormatTemplate97 = 1wdFormatText = 2wdFormatTextLineBreaks = 3wdFormatUnicodeText = 7wdFormatWebArchive = 9wdFormatXML = 11wdFormatXMLDocument = 12wdFormatXMLDocumentMacroEnabled = 13wdFormatXMLTemplate = 14wdFormatXMLTemplateMacroEnabled = 15wdFormatXPS = 18

The corresponding file format should be displayed literally. if you are in office 2003, you may not be able to support so many formats. Two formats are available for converting word files to html: wdFormatHTML and wdFormatFilteredHTML (corresponding to numbers 8 and 10). The difference is that if the format is wdFormatHTML, the formulas and other ole objects in the word file will be stored in wmf format, and the formula image will be stored in gif format if wdFormatFilteredHTML is used, in addition, it can be seen that the HTML generated by wdFormatFilteredHTML is much cleaner than wdFormatHTML.

Of course, you can also use any language to call office APIs through com, such as PHP.

from win32com import client as wcword = wc.Dispatch('Word.Application')doc = word.Documents.Open(r'c:/test1.doc')doc.SaveAs('c:/test1.text', 4)doc.Close()

Import restrings = open (r 'C: \ test1.text', 'r '). read () result = re. findall ('\ (\ s * [A-D] \ s * \) | \ (\ xa1 * [A-D] \ xa1 *\) | \ (\ s * [A-D] \ s * \) | \ (\ xa1 * [A-D] \ xa1 * \) ', strings) chan = re. sub ('\ (\ s * [A-D] \ s * \) | \ (\ xa1 * [A-D] \ xa1 *\) | \ (\ s * [A-D] \ s * \) | \ (\ xa1 * [A-D] \ xa1 * \) ',' () ', strings) question = open (r 'C: \ question ', 'A +') question. write (chan) question. close () answer = open (r 'C: \ answeronly ', 'A +') for I, a in enumerate (result): m = re. search ('[A-D] ', A) answer. write (str (I + 1) + ''+ m. group () + '\ n') answer. close () chan = re. sub (r' \ xa3 \ xa8 \ s * [A-D] \ s * \ xa3 \ xa9 ',' () ', strings) # Don't (), it is easy to cause ambiguity.

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.