Using readtable with Word document and special formatting (superscripts)
4 次查看(过去 30 天)
显示 更早的评论
When reading a table from a Word document, readtable seems to treat superscript content in the same way as other content.
For example, if a row in a table contains , readtable returns Lunar21, which is a problem. Superscripts are often used to point to footnotes. Is there an option for readtable to ignore superscript and other such characters, or any other workaround?
采纳的回答
Shlok
2024-10-16
Hi AR,
I understand that you are encountering issues while reading text with superscript characters using the “readtable” function. To address this issue, I would suggest utilizing an ActiveX session with Microsoft Word. This approach will allow you to remove any superscript characters before attempting to read the table. Refer the following steps to do so:
- Using MATLAB, start an ActiveX session with Word and load the document.
wordApp = actxserver('Word.Application');
wordApp.Visible = true; % You can keep the Microsoft Word visible to debug in case of any errors
doc = wordApp.Documents.Open('path_to_Temp_tableAttempt.docx');
- Now loop through the tables and, within each cell, iterate through each character to check for superscripts. If a superscript character is encountered, delete it:
for i = 1:doc.Tables.Count
table = doc.Tables.Item(i);
for row = 1:table.Rows.Count
for col = 1:table.Columns.Count
try
cell = table.Cell(row, col);
cellRange = cell.Range;
% Loop through each character in the cell
for charIndex = cellRange.Characters.Count:-1:1
char = cellRange.Characters.Item(charIndex);
% Check if the character is superscript
if char.Font.Superscript
% Remove the superscript character
char.Delete();
end
end
% To handle merged columns
catch ME
disp(['Error processing cell at row ' num2str(row) ', column ' num2str(col) ': ' ME.message]);
end
end
end
end
- Then save this updated file and close the ActiveX session.
doc.SaveAs2('path_to_updated_Temp_tableAttempt.docx ');
doc.Close();
wordApp.Quit();
By following these steps, you will be able to create an updated document free of superscript characters, making it easier to read the table using the “readtable” function.
To know more about “actxserver” function you can refer to the following MATLAB documentation link:
Hope this helps.
3 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!