简体   繁体   中英

Python pywin32 (win32com.client) responses wrong table before Microsoft Word document ready after open()

I found the table python read from MS Word document was wrong but can be correct if wait for a while so I make this experiment to look into the problem:

1. open a Word .docx document, it has total 26 tables.
2. Repeatedly print and see how many tables in this document? 
   Surprisingly this number is changing!
3. So the question is: how to know if the document is ready after .Open() ?

This is the code for the experiment:

import time 
import win32com.client
word_app = win32com.client.Dispatch("Word.Application")

doc = word_app.Documents.Open(FileName = pathname, ConfirmConversions = False,NoEncodingDialog = True,Revert = True)
for i in range(40):
    print("%d:%d " % (i, doc.tables.count), end="")
    time.sleep(0.1)
doc.Close()

Result:

 0:24 1:24 2:24... there were 24 tables at first, wrong! should be 26 
12:25 13:25    ... then it became 25
26:26 27:26 28:26 ... then become 26 which is finally correct

The document is ready as soon as the Open method is done. The Word object model doesn't provide any properties or method to check whether the document is loaded and initialized like in case of HTML web pages (web browsers).

This is my workaround

import time, docx, win32com.client
word_app = win32com.client.Dispatch("Word.Application")

doc_lib = docx.Document(pathname)
doc_com = word_app.Documents.Open(FileName = pathname)
while len(doc_lib.tables) != doc_com.tables.count:
    # need to wait probably a minute for doc_com to be really ready
    time.sleep(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM