if table3.cell(i,j).text.blod==True: AttributeError: 'str' object has no attribute 'blod'
时间: 2023-06-23 19:48:09 浏览: 50
这个错误是因为你在一个字符串对象上使用了`blod`属性,但是字符串对象没有`blod`属性。实际上,你需要使用`python-docx`库中的`Text`对象来检查文本的粗体属性。以下是一个修改后的例子,演示如何在一个Word表格中查找粗体字:
```python
from docx import Document
# 打开Word文档
doc = Document('example.docx')
# 获取第三个表格
table3 = doc.tables[2]
# 遍历表格中的所有单元格
for i in range(len(table3.rows)):
for j in range(len(table3.columns)):
# 获取单元格文本
cell_text = table3.cell(i,j).text
# 检查文本是否为粗体
for paragraph in table3.cell(i,j).paragraphs:
for run in paragraph.runs:
if run.bold:
print(f"Bold text found in cell ({i},{j}): '{cell_text}'")
```
在这个例子中,我们首先获取了文档中的第三个表格,并遍历表格中的所有单元格。对于每个单元格,我们获取单元格中的文本,并使用`runs`属性检查文本中是否存在粗体字。注意,我们需要遍历单元格中的所有paragraphs和runs来检查文本的粗体属性。如果找到粗体字,则打印出单元格的行列和文本。