How Python reads a Word document

Source: Internet
Author: User

This example describes how Python reads a Word document. Share to everyone for your reference. Specifically as follows:

First download the installation win32com

?

1 2 3 4 5 6 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 the normal R way, so that Python can read it in R, written as

?

1 Doc. SaveAs (' C:/test ', 4)

Note: The file suffix txt (although no suffix is specified) is automatically generated when the system is executed.

Under the XP system, you should,

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The > Open (R ' C:text ', ' r ') wdformatdocument = 0 WdFormatDocument97 = 0 Wdformatdocumentdefault = wdFormatDOSText = 4 wdFormatDOSTextLineBreaks = 5 Wdformatencodedtext = 7 WdFormatFilteredH TML = Wdformatflatxml = wdformatflatxmlmacroenabled = Wdformatflatxmltemplate = Wdformatflatxmltemplatemacroen abled = wdformathtml = 8 Wdformatpdf = wdFormatRTF = 6 wdFormatTemplate = 1 WdFormatTemplate97 = 1 Wdformattext = 2 Wdformattextlinebreaks = 3 Wdformatunicodetext = 7 Wdformatwebarchive = 9 Wdformatxml = one wdformatxmldocument = one wdForm atxmldocumentmacroenabled = Wdformatxmltemplate = wdformatxmltemplatemacroenabled = WdFormatXPS = @

should be able to correspond to the corresponding file format literally, if you are Office 2003 may not support so many formats. Word file to HTML there are two formats optional wdformathtml, wdformatfilteredhtml (corresponding to the number 8, 10), the difference is if the wdformathtml format, OLE objects, such as formulas in Word files, will be stored in WMF format, while wdformatfilteredhtml will be stored in GIF format, and visual can see the HTML generated with wdformatfilteredhtml Obviously much cleaner than wdformathtml.

Of course, you can also use any language to invoke the Office API, such as PHP, through COM.

?

1 2 3 4 5 From win32com import client as WC Word = WC. Dispatch (' Word.Application ') doc = Word. Documents.Open (R ' C:/test1.doc ') doc. SaveAs (' C:/test1.text ', 4) doc. Close ()

?

1 2 3 4 5 6 7 8 9 10 11 12 13-14 Import re Strings=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 ' xa3xa8s*[a-d]s*xa3xa9 ', ' () ', Strings) #不要 (), which is likely to cause ambiguity.

I hope this article will help you with your Python programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.