self.sign_A = 0
for row_A in table_A.Rows:
if row_A.Cells(1).Range.Text.strip() == "1.0":
self.sign_A = 1
print(self.sign_A)
if 后面不执行
【以下回答由 GPT 生成】
问题原因是在使用win32库查询表格时,使用的是COM对象的方法,而不是直接调用Python的方法。可以通过以下步骤来解决这个问题:
pywin32
库。如果没有安装,可以使用以下命令来安装:pip install pywin32
win32com.client
模块来创建一个Word应用程序对象,然后打开需要查询的Word文档。代码如下所示:import win32com.client
# 创建Word应用程序对象
word_app = win32com.client.Dispatch("Word.Application")
# 打开Word文档
document = word_app.Documents.Open("your_document_path")
table = document.Tables(1)
Columns
和Rows
属性来遍历表格的列和行。修改你的代码来检查第一列是否包含"1.0",并设置标志变量。代码如下所示:sign_A = 0
for row_A in table.Rows:
if row_A.Cells(1).Range.Text.strip() == "1.0":
sign_A = 1
print(sign_A)
# 关闭文档
document.Close()
# 退出应用程序
word_app.Quit()
请注意,以上代码只是一个示例,你需要根据自己的实际情况进行调整。将所有的代码整合起来,你可以得到以下完整的解决方案:
import win32com.client
# 创建Word应用程序对象
word_app = win32com.client.Dispatch("Word.Application")
# 打开Word文档
document = word_app.Documents.Open("your_document_path")
# 定位需要查询的表格
table = document.Tables(1)
# 判断表格的第一列是否包含"1.0"
sign_A = 0
for row_A in table.Rows:
if row_A.Cells(1).Range.Text.strip() == "1.0":
sign_A = 1
print(sign_A)
# 关闭文档
document.Close()
# 退出应用程序
word_app.Quit()
希望以上解决方案能够帮助你解决问题。如果还有其他问题,请随时提问。
【相关推荐】