Using readtable with Word document and special formatting (superscripts)

4 ビュー (過去 30 日間)
AR
AR 2024 年 3 月 7 日
コメント済み: AR 2024 年 10 月 18 日
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 件のコメント
AR
AR 2024 年 3 月 7 日
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 2024 年 3 月 8 日
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.

サインインしてコメントする。

採用された回答

Shlok
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 件のコメント
Walter Roberson
Walter Roberson 2024 年 10 月 18 日
Possibly calling methods might help.
AR
AR 2024 年 10 月 18 日
Thank you. I will try this out.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by