I have a table in Word filled with different texts, and some equations using Microsoft Equation 3.0.
I am currently trying to read the text from the table and create an Excel sheet with the same table.
Is there some way of normalizing the equations in Word into text?
If not, does somebody know how I can identify the equation to bypass it?
My current code for reading the table is this:
word = win32.gencache.EnsureDispatch('Word.Application')
word.Visible = False
raw_files = glob('*.docx')
xl = win32.gencache.EnsureDispatch('Excel.Application')
ss = xl.Workbooks.Add()
for f in raw_files:
doc = word.ActiveDocument
for x in xrange(1, doc.Paragraphs.Count+1):
oText = doc.Paragraphs(x)
if oText.Range.Tables.Count >0 :
ph = ss.ActiveSheet
for r in xrange(1, oText.Range.Tables(1).Rows.Count):
for c in xrange(1, oText.Range.Tables(1).Columns.Count):
if oText.Range.Tables(1).Cell(r,c).Range.Text != None:
ph.Cells(r+2,c).Value = oText.Range.Tables(1).Cell(r,c).Range.Text
If you are running Word 2010 (possibly 2007) you could check if there is an equation in the cell in this way (this is complete loop for each cell in table which you can easily convert to your needs, Word-VBA code, tried and tested for Word 2010):
Dim eqCell As Cell For Each eqCell In ActiveDocument.Tables(1).Range.Cells If eqCell.Range.OMaths.Count > 0 Then 'if there is any equation this if statement will return true 'so, this cell should be bypassed '**EDIT** how to get row and column number of this cell: Dim rowNo As Long Dim colNo As Long rowNo = eqCell.Range.Information(wdEndOfRangeRowNumber) colNo = eqCell.Range.Information(wdEndOfRangeColumnNumber) Debug.Print rowNo, colNo '**END OF EDIT** End If Next
You could try normalize your equation referring to some properties of OMathFunction Object. Unfortunately, I have no experience in this area.
To use this solution within your code you could implement it in few possible ways:
a) to check if there is any equation in the table:
If oText.Range.Tables(1).Range.OMath.Count > 0 Then ... '>>here is
b) to check if there is equation in your cell:
If oText.Range.Tables(1).Cell(r,c).Range.OMath.Count > 0 Then ... '>>here is