reading tables from .doc word file

조회 수: 22 (최근 30일)
vignashwaran s
vignashwaran s 2019년 8월 28일
댓글: João 2022년 11월 7일
hi ... i can able to read a .doc file but it have some tables when i try to, read all txt in table comes in a single cell ..any one having code for reading table from word file .doc

채택된 답변

Cris LaPierre
Cris LaPierre 2019년 8월 28일
Here's some starter code for you. Be aware that this code assumes that, if there are multiple tables, they are all the same size.
%% 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
  댓글 수: 2
vignashwaran s
vignashwaran s 2019년 8월 30일
Thanks Cris.. thanks for repying, it's working.
João
João 2022년 11월 7일
wdmain11.chm dependency , be aware.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by