简体   繁体   English

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

[英]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:我发现从 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() ?

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

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)

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

相关问题 安装pywin32 lib后仍然出现错误“ ImportError:没有名为win32com.client的模块” - Still getting an error “ImportError: No module named win32com.client” after installing pywin32 lib 使用 Pywin32 (win32com.client) 获取 Outlook 组日历 - Get Outlook Group Calendars using Pywin32 (win32com.client) ImportError:没有名为 win32com.client 的模块(我确定我安装了 pywin32) - ImportError: No module named win32com.client (I'm sure I have pywin32 installed) 无法使用win32com.client打开只读Microsoft Word文件 - Cannot open read-only Microsoft Word files with win32com.client 使用 win32com.client 读取 MS Word 表格 - Using win32com.client to Read MS Word Table Python win32com.client和outlook - Python win32com.client and outlook Python win32com.client和“with”语句 - Python win32com.client and “with” statement 如果某个任务正在任务管理器中运行,如何在python中使用pywin或win32com.client进行检查? - How can I check with pywin or win32com.client in python if a certain task is running in task manager? 只能导入带有IDLE的win32com.client。 设置pywin32需要做哪些额外的工作? - Can only import win32com.client with IDLE. What extra work do i need to set up pywin32? 通过pywin32将pandas数据帧写入word文档表 - Writing a pandas dataframe to a word document table via pywin32
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM