Using readtable with Word document and special formatting (superscripts)

4 vues (au cours des 30 derniers jours)
AR
AR le 7 Mar 2024
Commenté : AR le 18 Oct 2024
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?
  6 commentaires
AR
AR le 7 Mar 2024
Thanks for the tip on attaching. Here's an example, although there are many more tables that are much larger and harder to work with.
AR
AR le 8 Mar 2024
Nevermind my original request. Although I still don't know how to directly remove special formatting while importing using readtable (if it's possible at all), I did manage to get the actxserver method working. Thanks.

Connectez-vous pour commenter.

Réponse acceptée

Shlok
Shlok le 16 Oct 2024
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 commentaires
Walter Roberson
Walter Roberson le 18 Oct 2024
Possibly calling methods might help.
AR
AR le 18 Oct 2024
Thank you. I will try this out.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by