Created
June 28, 2021 02:22
-
-
Save rdapaz/1afd637929d775eb3bde1060f9a1bf66 to your computer and use it in GitHub Desktop.
Re-Number Table Index Columns in Word
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AFQ210311450994 | |
import re | |
import win32com.client as w32 | |
path = r'<path goes here>' | |
wdApp = w32.gencache.EnsureDispatch('Word.Application') | |
wdApp.Visible = True | |
doc = wdApp.Documents.Open(path) | |
def StripBellChars(s): | |
new_s = s.replace('\A', '') | |
new_s = new_s.strip() | |
return new_s | |
def DoesNotLookLikeNumber(s): | |
return re.search('^\d+.*', s) == False | |
rex = re.compile(r'\.?[0-9]+\.?') | |
for idx in range(1, doc.Tables.Count + 1): | |
tbl = doc.Tables(idx) | |
referenceCell = StripBellChars(tbl.Cell(1,1).Range.Text) | |
try: | |
if re.search(r'(id|item|step)', referenceCell, re.IGNORECASE): | |
for row in range(1, tbl.Rows.Count + 1): | |
val = tbl.Cell(row,1).Range.Text.strip()[:-1] | |
if row >= 1: | |
if val and not rex.search(val): | |
pass | |
else: | |
tbl.Cell(row, 1).Range.Text = f"({row-1})" | |
except: | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment