How Python reads a Word document

Source: Internet
Author: User
The example in this article describes how Python reads a Word document. Share to everyone for your reference. Specific as follows:

First Download 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 ()

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 = 0wdformatdocument97 = 0wdFormatDocumentDefault = 16wdFormatDOSText = 4wdFormatDOSTextLineBreaks = 5wdFormatEncodedText = 7wdFormatFilteredHTML = 10wdFormatFlatXML = 19wdFormatFlatXMLMacroEnabled = 20wdFormatFlatXMLTemplate = 21wdFormatFlatXMLTemplateMacroEnabled = 22wdFormatHTML = 8wdFormatPDF = 17wdFormatRTF = 6wdFormatTemplate = 1wdformattemplate97 = 1wdFormatText = 2wdFormatTextLineBreaks = 3wdForm Atunicodetext = 7wdFormatWebArchive = 9wdFormatXML = 11wdFormatXMLDocument = 12wdFormatXMLDocumentMacroEnabled = 13wdFormatXMLTemplate = 14wdFormatXMLTemplateMacroEnabled = 15wdFormatXPS = 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.

Of course you can also use COM to invoke Office APIs in any language, 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) #不要 (), easy to cause ambiguity.

Hopefully this article will help you with Python programming.

  • 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.