繁体   English   中英

Python pywin32(win32com.client)在打开()后准备好Microsoft Word文档之前响应错误表

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

我发现从 MS Word 文档中读取的表 python 是错误的,但如果等待一段时间可能是正确的,所以我做了这个实验来调查问题:

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() ?

这是实验的代码:

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

结果:

 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

Open 方法一完成,文档就准备好了。 Word 对象模型不提供任何属性或方法来检查文档是否已加载和初始化,就像在 HTML 网页(Web 浏览器)的情况下一样。

这是我的解决方法

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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