批量将excel中的图片填入word特定表格中

本文介绍了如何使用MATLAB编程批量将Excel工作簿中的图片提取出来,转换为.zip文件,然后解压到特定目录。接着,程序会打开Word文档,定位到特定表格并在倒数第二行逐个插入图片。
摘要由CSDN通过智能技术生成

步骤:

1、将excel中的图片提取出来保存在一个文件夹中;
2、把图片插入word特定表格。

将excel中的图片提取出来保存在一个文件夹中

思路:把.xlsx文件改为.zip文件再解压,图片在xl\media\目录下:

%%.xlsx文件改为.zip文件
[Files, Path] = uigetfile({'*.xls;*.xlsx','Excel Files (*.xls;*.xlsx)';'*.*', 'All Files (*.*)'}, 'MultiSelect','on');

for i = 1:length(Files)
    oldFileName = fullfile(Path, Files{i});
    newFileName = fullfile(Path, [erase(Files{i}, '.xlsx'), '.zip']);
    movefile(oldFileName, newFileName);
end
%% 解压缩
[zipFiles, zipFolder] = uigetfile({'*.zip','Zip Files (*.zip)';'*.*', 'All Files (*.*)'}, 'MultiSelect','on'); % 选择文件

Index = 1;
for Index = 1:length(zipFiles)
    % ZIP文件路径
    zipFilePath = fullfile(zipFolder, zipFiles{Index});
    
    % 结果路径
    targetFolder = fullfile(zipFolder, '已解压',zipFiles{Index});
    if ~exist(targetFolder, 'dir')
        mkdir(targetFolder);
    end

    % 解压缩
    unzip(zipFilePath, targetFolder);
end

把图片插入word特定表格

%% 把图片插入word特定表格
[filename_in, pathname] = uigetfile({'*.doc;*.docx;*.docm','Word Files (*.doc,*.docx,*.docm)';'*.*', 'All Files (*.*)'}, 'MultiSelect','on');

tic
word = actxserver('Word.Application');
word.Visible = 0;
word.DisplayAlerts = 0;

Index = 1;
for Index = 1:length(filename_in)

    doc = word.Documents.Open(fullfile(pathname,filename_in{Index}));
    
    allTables = doc.Tables;
    
    % table1 = allTables.Item(1);
    targetText = '查找文本';
    table1 = [];
    for i = 1:allTables.Count
        currentTable = allTables.Item(i);
        if ~isempty(strfind(currentTable.Range.Text, targetText))
            table1 = currentTable;
            break;
        end
    end
    table1Selection = word.Selection;
    
    % 图片列表
    [~, BaseName, ~] = fileparts(filename_in{Index});
    imageFilespath = fullfile('D:\路径\图片',BaseName,'xl\media\*.jpeg'); 
    imageFiles = dir(imageFilespath);
    if length(imageFiles) < 2
        disp(filename_in{Index})
    end

    % 遍历图片文件并插入表格
    for i = 1:length(imageFiles)
        
        imagePath = fullfile('D:\路径\图片',BaseName,'xl\media', imageFiles(i).name);
        
        % 设置图片的长宽
        width = 224.8;  % 设置宽度(单位:磅)
        height = 169.8; % 设置高度(单位:磅)
        
    
        % 插入图片(倒数第二行,第i列)
        table1.Cell(table1.Rows.Count-1, i).Range.InlineShapes.AddPicture(imagePath);
        table1.Cell(table1.Rows.Count-1, i).Range.InlineShapes.Item(table1.Cell(table1.Rows.Count-1, i).Range.InlineShapes.Count).Width = width;
        table1.Cell(table1.Rows.Count-1, i).Range.InlineShapes.Item(table1.Cell(table1.Rows.Count-1, i).Range.InlineShapes.Count).Height = height;
       
    end

    doc.SaveAs2(cell2mat(fullfile(pathname,'已改\',filename_in(Index))));
    doc.Close ();
end
word.Quit ();
delete(word);
toc
  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用VBA宏来实现将Excel图片和文本批量填入Word文件。具体步骤如下: 1. 在Excel需要导入图片和文本,复制它们到剪贴板。 2. 打开Word文件,选择需要填入内容的位置。 3. 按下Alt+F11打开VBA编辑器,选择Tools->References,勾选Microsoft Word xx.x Object Library和Microsoft Excel xx.x Object Library。 4. 在VBA编辑器插入以下代码: Sub InsertExcelDataToWord() Dim wdApp As Word.Application Dim wdDoc As Word.Document Dim xlApp As Excel.Application Dim xlWb As Excel.Workbook Dim xlWs As Excel.Worksheet Dim xlRng As Excel.Range Dim wdRng As Word.Range Dim wdShape As Word.InlineShape '打开Word文件 Set wdApp = New Word.Application Set wdDoc = wdApp.Documents.Open("C:\Test.docx") '打开Excel文件 Set xlApp = New Excel.Application Set xlWb = xlApp.Workbooks.Open("C:\Test.xlsx") Set xlWs = xlWb.Worksheets("Sheet1") '复制Excel图片和文本到Word文件 For Each xlRng In xlWs.UsedRange Set wdRng = wdDoc.Range(xlRng.Address) If xlRng.Value <> "" Then wdRng.Text = xlRng.Value End If If xlRng.Comment IsNot Nothing Then wdRng.Comments.Add xlRng.Comment.Text End If If xlRng.Hyperlinks.Count > 0 Then wdRng.Hyperlinks.Add wdRng, xlRng.Hyperlinks(1).Address End If If xlRng.Pictures.Count > 0 Then Set wdShape = wdRng.InlineShapes.AddPicture(xlRng.Pictures(1).Filename) wdShape.Width = xlRng.Pictures(1).Width wdShape.Height = xlRng.Pictures(1).Height End If Next xlRng '保存并关闭文件 wdDoc.Save wdDoc.Close xlWb.Close wdApp.Quit xlApp.Quit '释放对象 Set wdShape = Nothing Set wdRng = Nothing Set wdDoc = Nothing Set wdApp = Nothing Set xlRng = Nothing Set xlWs = Nothing Set xlWb = Nothing Set xlApp = Nothing End Sub 5. 修改代码的文件路径和工作表名称,运行宏即可将Excel图片和文本批量填入Word文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值