Borrar filtros
Borrar filtros

How to Read the tables from Word document (*.docx) with table headings?

23 visualizaciones (últimos 30 días)
%% 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)??

Respuestas (1)

Prateek
Prateek el 25 de Nov. de 2022
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.

Categorías

Más información sobre Debugging and Analysis en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by