How to Read the tables from Word document (*.docx) with table headings?
39 次查看(过去 30 天)
显示 更早的评论
%% open the doc
word = actxserver('Word.Application');
% Open/Select Word file
[filename_in, pathname] = uigetfile({'*.doc;*.docx;*.docm','Word Files (*.doc,*.docx,*.docm)'; ...
'*.*', 'All Files (*.*)'}, 'Select a file');
document = word.documents.Open(fullfile(pathname,filename_in));
r=0;
tbl_cnt = document.Tables.Count;
% Loop through each table in the document
for tbl = 1 : tbl_cnt
row_cnt = document.Tables.Item(tbl).Rows.Count;
col_cnt = document.Tables.Item(tbl).Columns.Count;
for row = 1 : row_cnt
r = r+1;
for col = 1 : col_cnt
% Pull the values from the table
cell_txt = strtrim(document.Tables.Item(tbl).Cell(row, col).Range.Text);
% Add each value to its own cell
tblVals{r,col} = cell_txt;
end
end
end
% Close the document.
document.Close;
% Close Word.
word.Quit;
% delete server object
delete(word);
clear word document
this script can be used to read the tables, but how to know the table Name (highlighted text in the attached image)??
0 个评论
回答(1 个)
Prateek
2022-11-25
Hello Bala,
I was able to execute the code posted by you, with a dummy Word file containing a table.
The contents of the table (in the Word file) can be seen in the following screenshot:
The contents of the "tblVals" cell array can be seen in the following screenshot:
Thus, the "tblVals" cell array contains the table headers as well and you can access these.
Hope this helps.
Regards,
Prateek.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Report Generator 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!