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.