I am new to Python and I am trying to check docx file for texts in table that have hidden attribute applied to them. If True, then I want to ignore that hidden text and replace any other text that matches my regex.
The thing is everything been good (replacement is working) until I added if i.font.hidden == True: condition thata seems to be incorrect.
The error I am getting: AttributeError: ‘int’ object has no attribute ‘font’
This is the code I have:
JavaScript
x
15
15
1
for table in doc.tables:
2
for row in table.rows:
3
for cell in row.cells:
4
for p in cell.paragraphs:
5
if True:
6
inline = p.runs
7
for i in range(len(inline)):
8
if True:
9
if i.font.hidden == True:
10
continue
11
else:
12
text = inline[i].text.replace(regx, 'Abcd')
13
14
inline[i].text = text
15
Advertisement
Answer
No need to access runs by index; you can iterate those directly:
JavaScript
1
8
1
for table in doc.tables:
2
for row in table.rows:
3
for cell in row.cells:
4
for p in cell.paragraphs:
5
for run in p.runs:
6
if not run.font.hidden:
7
run.text = run.text.replace(regx, 'Abcd')
8