필터 지우기
필터 지우기

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

조회 수: 23 (최근 30일)
Bala Murugan S P
Bala Murugan S P 2022년 11월 21일
편집: Bala Murugan S P 2022년 11월 28일
%% 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)??

답변 (1개)

Prateek
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.

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by