找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 14873|回复: 9

(完结)+50,批量将JPG合并成PDF

  [复制链接]
发表于 2024-8-6 18:09:56 | 显示全部楼层 |阅读模式
本帖最后由 hxc1995 于 2024-8-7 09:44 编辑

JPG名称为中文加数字,5级目录下,批量将按中文名称相同合并成一个PDF。
例如照片名称为:胡1.jpg、胡2.jpg、胡3.jpg、李.jpg、张1.jpg、张2.jpg;
成果为:胡.pdf、李.pdf、张.pdf
发表于 2024-8-6 20:18:52 | 显示全部楼层
本帖最后由 zaqmlp 于 2024-8-7 09:22 编辑

imagemagick下载

  1. /*&cls&echo off&cd /d "%~dp0"
  2. set "exefile=D:\ImageMagick\magick.exe"
  3. if not exist "%exefile%" (echo;"%exefile%" not install or path error&goto end)
  4. for /f "delims=" %%a in ('dir /ad/b/s') do (
  5.     pushd "%%a"
  6.     for /f "delims=" %%b in ('dir /a-d/b *.jpg 2^>nul^|cscript -nologo -e:jscript "%~f0"') do (
  7.         echo;"%%b*.jpg" --^> "%%b.pdf"
  8.         "%exefile%" "%%b*.jpg" "%%b.pdf"
  9.         >nul chcp 936
  10.     )
  11.     popd
  12. )
  13. :end
  14. pause
  15. exit
  16. */
  17. var cn={};
  18. while(!WSH.StdIn.AtEndOfStream){
  19.     var str=WSH.StdIn.ReadLine();
  20.     var m=str.match(/[\u4e00-\u9fa5]+/);
  21.     if(m && !(m[0] in cn)){WSH.echo(m[0]);cn[m[0]]=1}
  22. }
复制代码
发表于 2024-8-6 20:47:00 | 显示全部楼层
本帖最后由 buyiyang 于 2024-8-6 20:56 编辑

遍历目录合并jpg为pdf,保存为ansi编码的bat
  1. <# :
  2. @echo off
  3. cd /d "%~dp0"
  4. for /f "delims=" %%i in ('dir /b /ad /s') do (
  5.     pushd "%%i"&&(
  6.     if exist *.jpg (
  7.         echo,%%i
  8.         powershell -c "gc '%~0' | Out-String | iex"
  9.         )
  10.     popd
  11.     )||set /p=%%i<nul
  12. )
  13. pause&exit
  14. #>

  15. [void][Reflection.Assembly]::LoadWithPartialName('System.Drawing')

  16. function Img2Pdf($paramImgGroup) {
  17.     $listImg = New-Object 'System.Collections.ArrayList'
  18.     ForEach ($file In $groups[$paramImgGroup]) {
  19.         [void]$listImg.Add([Drawing.Image]::FromFile($file))
  20.     }

  21.     $docPrint = New-Object 'Drawing.Printing.PrintDocument'
  22.     $settingsPrint = New-Object 'Drawing.Printing.PrinterSettings'
  23.     $settingsPrint.PrinterName = 'Microsoft Print to PDF'
  24.     $settingsPrint.PrintToFile = $true
  25.     $settingsPrint.PrintFileName = $paramImgGroup + '.pdf'
  26.     $docPrint.PrinterSettings = $settingsPrint
  27.     $docPrint.DefaultPageSettings.Landscape = $true

  28.     $docPrint.add_PrintPage({
  29.         $imgCurrent = $listImg[0]
  30.         $ratioWidth = $_.MarginBounds.Width / $imgCurrent.Width
  31.         $ratioHeight = $_.MarginBounds.Height / $imgCurrent.Height
  32.         $ratioSelected = [Math]::Min($ratioWidth, $ratioHeight)
  33.         $widthNew = [int]($imgCurrent.Width * $ratioSelected)
  34.         $heightNew = [int]($imgCurrent.Height * $ratioSelected)
  35.         $_.Graphics.DrawImage($imgCurrent, ($_.MarginBounds.Left + ($_.MarginBounds.Width - $widthNew) / 2), ($_.MarginBounds.Top + ($_.MarginBounds.Height - $heightNew) / 2), $widthNew, $heightNew)
  36.         $imgCurrent.Dispose()
  37.         $listImg.RemoveAt(0)
  38.         If ($listImg.Count -gt 0) { $_.HasMorePages = $true } Else { $_.HasMorePages = $false }
  39.     })

  40.     $docPrint.Print()
  41.     $docPrint.Dispose()
  42. }

  43. $files = Get-ChildItem -Filter "*.jpg"
  44. $groups = @{}

  45. ForEach ($file In $files) {
  46.     $partChinese = [regex]::Match($file.Name, '^[\u4e00-\u9fff]+').Value
  47.     If ($partChinese) {
  48.         If (-not $groups.ContainsKey($partChinese)) {
  49.             $groups[$partChinese] = @()
  50.         }
  51.         $groups[$partChinese] += $file
  52.     }
  53. }

  54. ForEach ($group In $groups.Keys) {
  55.     Write-Output "分组:$group"
  56.     Img2Pdf -paramImgGroup $group
  57. }
复制代码
发表于 2024-8-7 00:03:43 | 显示全部楼层
我也用magick写了一个,第二次findstr 一直失败,找了很久的原因,最终发现magick会改变代码页,仅在此分享一下这个“趣事”。
  1. @echo off
  2. rem 保存为ansi
  3. cd /d "%~dp0"
  4. for /r /d %%d in (*) do (
  5.         pushd "%%d"
  6.         for /f "tokens=1* delims=1234567890." %%a in ('2^>nul dir /b /a-d *.jpg') do (
  7.                 if not exist "%%a.pdf" (
  8.                         setlocal enabledelayedexpansion
  9.                         for /f "delims=" %%i in ('dir /b /a-d *.jpg ^|findstr /rixc:"%%a[0-9]*\.jpg"') do (
  10.                                 set name=%%i
  11.                                 set "num=!name:%%a=!"
  12.                                 set /a num+=10000
  13.                                 set #!num!=%%i
  14.                         )
  15.                         for /f "tokens=1* delims==" %%x in ('set #') do (
  16.                                 set str=!str! "%%y"
  17.                         )
  18.                         magick !str! "%%a.pdf"
  19.                         chcp 936 >nul
  20.                         endlocal
  21.                 )
  22.         )
  23.         popd
  24. )
  25. pause
复制代码
 楼主| 发表于 2024-8-7 08:56:23 | 显示全部楼层
回复 3# buyiyang
大佬,你写的合出来的PDF排版不对,图片是竖向的,合成的PDF是横向的,有办法解决吗
 楼主| 发表于 2024-8-7 09:08:23 | 显示全部楼层
回复 2# zaqmlp
第一个文件夹成功了,后面一直报错
发表于 2024-8-7 09:27:05 | 显示全部楼层
回复 6# hxc1995 已修改
 楼主| 发表于 2024-8-7 09:44:20 | 显示全部楼层
本帖最后由 hxc1995 于 2024-8-7 09:47 编辑

回复 7# zaqmlp
OK,没问题了,已支付,能附赠一个批量修改照片名称中文部分吗?例如批量将张1、张2、张3改成陈1、陈2、陈3,还是5级目录下
发表于 2024-8-7 10:11:42 | 显示全部楼层
回复 8# hxc1995
  1. <# :
  2. cls&echo off&cd /d "%~dp0"&rem bat存为ANSI/GB2312编码
  3. path %SYSTEMROOT%\System32\WindowsPowerShell\v1.0;%path%
  4. set "current=%cd%"
  5. powershell -NoProfile -ExecutionPolicy bypass "Get-Content -literal '%~f0'|Out-String|Invoke-Expression"
  6. pause
  7. exit
  8. #>
  9. $rp=@("张=陈","刘=王");
  10. $current=($env:current).trimend('\');
  11. $files=@(dir -literal $current -recurse|?{('.jpg' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
  12. for($i=0;$i -lt $files.length;$i++){
  13.     $m=[regex]::match($files[$i].BaseName, '^[\u4e00-\u9fff]+');
  14.     if($m.Success){
  15.         for($j=0;$j -lt $rp.length;$j++){
  16.             $kw=$rp[$j].split('=');
  17.             if($m.groups[0].value -eq $kw[0]){
  18.                 $newname=$files[$i].BaseName.replace($kw[0], $kw[1])+$files[$i].Extension;
  19.                 $newfile=$files[$i].Directory.FullName.trimend('\')+'\'+$newname;
  20.                 write-host ($files[$i].FullName+' --> '+$newname);
  21.                 move-item -literal $files[$i].FullName $newfile -ErrorAction SilentlyContinue;
  22.                 break;
  23.             }
  24.         }
  25.     }
  26. }
复制代码
 楼主| 发表于 2024-8-7 15:38:15 | 显示全部楼层
回复 9# zaqmlp
太感谢了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 01:47 , Processed in 0.019887 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表