当前位置: 首页 > news >正文

03 以上版本 Excel 文件解压替换图片

replace_images_zip_method.ps1 代码如下

param ([Parameter(Mandatory=$true)][string]$ExcelPath,[Parameter(Mandatory=$true)][string]$ImagesFolder,[Parameter(Mandatory=$false)][string]$OutputSuffix = "_new"
)try {# 1. Validate inputsif (-not (Test-Path $ExcelPath)) { throw "Excel file not found: $ExcelPath" }if (-not (Test-Path $ImagesFolder)) { throw "Images folder not found: $ImagesFolder" }$ExcelPath = (Get-Item $ExcelPath).FullName$ImagesFolder = (Get-Item $ImagesFolder).FullName# 2. Prepare output file$dir = Split-Path $ExcelPath -Parent$name = [System.IO.Path]::GetFileNameWithoutExtension($ExcelPath)$ext = [System.IO.Path]::GetExtension($ExcelPath)$outputExcelPath = Join-Path $dir "${name}${OutputSuffix}${ext}"Write-Host "Creating copy: $outputExcelPath" -ForegroundColor CyanCopy-Item $ExcelPath $outputExcelPath -Force# 3. Load ZipFile assemblyAdd-Type -AssemblyName System.IO.Compression.FileSystem# 4. Open the copy as a Zip archive$zip = [System.IO.Compression.ZipFile]::Open($outputExcelPath, [System.IO.Compression.ZipArchiveMode]::Update)# 5. Iterate through images in the folder and replace in zip$imageFiles = Get-ChildItem $ImagesFolder -File$count = 0# Create a map of BaseName -> ZipEntry for faster lookup$zipEntries = @{}foreach ($entry in $zip.Entries) {if ($entry.FullName -like "xl/media/*") {$baseName = [System.IO.Path]::GetFileNameWithoutExtension($entry.Name)$zipEntries[$baseName] = $entry}}foreach ($img in $imageFiles) {$imgBaseName = [System.IO.Path]::GetFileNameWithoutExtension($img.Name)# Look for matching entry by BaseName (ignoring extension)if ($zipEntries.ContainsKey($imgBaseName)) {$targetEntry = $zipEntries[$imgBaseName]$targetName = $targetEntry.NameWrite-Host "  Replacing: $targetName (with $($img.Name))" -ForegroundColor Green# Delete existing entry$entryPath = $targetEntry.FullName$targetEntry.Delete()# Create new entry with the ORIGINAL name (preserving extension in Zip)# This "tricks" Excel into accepting the new content (e.g. PNG) as the old file type (e.g. JPEG)# which usually works fine and keeps XML relationships valid.[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $img.FullName, $entryPath)# Update map to avoid double processing (though unlikely with unique names)$zipEntries.Remove($imgBaseName)$count++} else {# Optional: Write-Host "  Skipping: $($img.Name) (no match in Excel)" -ForegroundColor DarkGray}}$zip.Dispose()Write-Host "`nSuccess! Replaced $count images." -ForegroundColor CyanWrite-Host "Output saved to: $outputExcelPath" -ForegroundColor Cyan} catch {Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
}

./replace_images_zip_method.ps1 example.xlsx ./imagefolder
结果生成example_new.xlsx
支持 WPS 嵌入图片。观察到 WPS 嵌入转浮动时,图片images目录下的编号会被打乱,反之亦然,但 dispimg 公式中显示的ID,在图片切换为浮动后,会在选择窗格中显示。

http://www.jsqmd.com/news/79180/

相关文章:

  • 【题解】Luogu P13977 数列分块入门 2
  • AI核心知识50——大语言模型之Scaling Laws(简洁且通俗易懂版)
  • MySQL 深分页查询优化实践与经验总结
  • P2014 [CTSC1997] 选课
  • 彻底讲清 MySQL InnoDB 锁机制:从 Record 到 Next-Key 的全景理解
  • 超越宣传:基于数据与案例的软件人才外包服务商价值评估指南
  • MCU的启动流程你了解么?
  • 电机多目标优化与灵敏度分析:探索电机性能提升之道
  • I2C通信最全面的讲解:从协议到硬件设计
  • 打造下一个爆款!专业短剧APP全栈开发解决方案,解锁万亿级市场红利
  • 毕业论文选题AI推荐:9大工具+热门方向合集
  • 【题解】Luogu P10752 [COI 2024] Sirologija
  • PFC2D预制裂隙巴西劈裂试验模拟:探索岩石破裂奥秘
  • Python字符串:别只用来打印!这5个高级用法让代码效率翻倍
  • PSRR仿真教程:解锁电路抗噪能力的密钥
  • C51_AH3144霍尔传感器
  • C51_74HC595串口转并口
  • 【题解】Atcoder ABC432 C
  • 赶due党救急!论文降重2小时搞定,不熬夜
  • 5 分钟快速入门 Gitlab CI/CD
  • 计算机论文模板推荐:8大平台+AI修改工具
  • 16 位 SAR ADC 逐次逼近型 ADC 模拟集成电路设计探秘
  • Lua语法深入1
  • 【题解】Luogu P13885 [蓝桥杯 2023 省 Java/Python A] 反异或 01 串
  • 期待回家,顺便写点年度总结
  • E No address added out of total 1 resolved地址绑定失败: No address added out of total 1 resolved errors:
  • 计算机论文题目推荐:8大平台+50例AI生成
  • 【笔记】Manacher
  • 八上期中考游记
  • C51_74HC165并口转串口