根据文件名批量合并PDF文件

根据文件名批量合并PDF文件

我正在尝试寻找一个可以根据文件名合并文件的程序/脚本。文件位于 1 个文件夹中(从 PDF24 打印到 PDF 的输出),名称如下例所示:

文件名布局:YYYY-MM-DD HH-MM-SS file name.pdf

例子:

2021-05-31 11-12-13 Microsoft Outlook - Memo Style.pdf
2021-05-31 11-12-15 Some another filename - string.pdf
2021-05-31 11-12-18 Some another filename - string.pdf
2021-05-31 11-12-25 Some another filename - string.pdf
2021-05-31 11-12-45 Some another filename - string.pdf
2021-05-31 11-13-21 Microsoft Outlook - Memo Style.pdf

我想要的是程序查看文件名,并从“Microsoft Outlook”(包括 Outlook)到下一个“Microsoft Outlook”(不包括)的每个文件中提取它们并合并。

我正在做的是打印一个 Outlook 文件及其(部分)附件,并且我想合并它们,因此每个 PDF 文件都是第 1 页上的一封邮件,并且在接下来的页面上有它的附件。

要求:

  • 这里重要的是附件的顺序正确,即按日期排序,最早的在最前面
  • 我希望通过邮件拆分 PDF 文件。合并后的每个 PDF 文件都是 1 个文件及其附件
  • 我实际上更喜欢手动脚本,因为它必须每周或每隔几天运行一次
  • 输出应自动保存到我选择的文件夹(源文件夹的子文件夹)
  • 我不太在意输出文件名。例如,它们可以是 001、002……

在发现这里的一些问题后,我查看了 PDFtk,但它要么无法做到,要么我对文档不够了解(这是一种非常现实的可能性)。

如果有人能提供帮助,我们将不胜感激。

附言:我已经可以把每个 PDF 合并成一个大文件了,但我希望把它们分开,因为这样我就可以自动打印和装订它们。这真是……节能。我想先合并后再分割也是可行的。

答案1

供将来参考,ComputerUser121212 发布的代码运行完美。

我已将其制作成批处理文件,使用以下链接作为帮助: https://stackoverflow.com/questions/4571244/creating-a-bat-file-for-python-script

我不精通批处理文件,所以我的代码可能不是最优的,但它可以工作。

    @echo on
rem = """

python -x "%~f0" %*
echo some more batch commands
goto :eof

"""
# Anything here is interpreted by Python

import os

files = os.listdir(".")
files.sort()

command_prefix = "pdftk "
command_args = ""
command_end = "cat output output1.pdf"
counter = 0

for file in files:
  if ".py" not in file:
    if "Microsoft Outlook" in file:
      if files.index(file) != 0:
        os.system(command_prefix + command_args + command_end)
      counter = counter + 1
      command_args = '"' + file + '"' + " "
      command_end = "cat output output" + str(counter) + ".pdf"
    else:
      command_args = command_args + '"' + file + '"' + " "

os.system(command_prefix + command_args + command_end)

将上面的代码粘贴到记事本中,保存为 pdf 文件夹内的 .bat 文件,即可完成。

答案2

以下 Python 代码应该可以解决问题。该代码在 Python 3.9 上运行并使用 PDFtk。它假设目录中的第一个文件包含“Microsoft Outlook”

import os

files = os.listdir(".")
files.sort()

command_prefix = "pdftk "
command_args = ""
command_end = "cat output output1.pdf"
counter = 0

for file in files:
  if ".py" not in file:
    if "Microsoft Outlook" in file:
      if files.index(file) != 0:
        os.system(command_prefix + command_args + command_end)
      counter = counter + 1
      command_args = '"' + file + '"' + " "
      command_end = "cat output output" + str(counter) + ".pdf"
    else:
      command_args = command_args + '"' + file + '"' + " "

os.system(command_prefix + command_args + command_end)

将其保存为文件并从与文件相同的目录中运行它

答案3

使用纯球棒完成相同工作的替代方法是……

@echo off 

Setlocal EnabledelayeDexpansion

set "_in_dir=Full\Path\To\your\pdf\Files\Folder"
set "_out_dir=Full\Path\To\Some\Temp\Folder\Output"

2>nul mkdir "!_out_dir!" & cd /d "!_in_dir!"
set _cnt=<nul & set "_results=!_in_dir!\output.pdf" 

for /f tokens^=* %%i in ('dir /od /a:a /b "*Microsoft?Outlook*.pdf"
')do set /a "_cnt+=1" && call %:^) "%%~fi" "000!_cnt!" "!_out_dir!"

pushd "Full\Path\To\your\pdfTK\Folder\bin"
pdftk.exe "!_out_dir!\*.pdf" cat output "!_results!"

%:^) 
if not "%~nx1"=="" (set "_new=%~2" & call move "%~1" "%~3\!_new:~-4!.pdf"
     exit /b) else (rmdir /s /q "!_out_dir!\." & popd & endlocal & goto :eOf)

1)编辑此脚本,将变量添加/设置到您将在 bat 过程中使用的各自路径,以及pdfTK.exe文件夹的相关完整路径

set "_in_dir=Full\Path\To\your\pdf\Files\Folder"
set "_out_dir=Full\Path\To\Some\Temp\Folder\Output"
...
pushd "Full\Path\To\your\pdfTK\Folder\bin"

2)for /f通过过滤感兴趣的名称列出您的文件,在一个循环中按日期排序(旧的优先) :

dir /od /a:a /b 

3)"!_in_dir!\%%~i %%~j %%~k"对于每个循环文件​​,增加一个计数器来在已经传递 pdf 文件路径和临时文件夹的函数内执行"!_out_dir!\."

set /a "_cnt+=1" && call %:^) "%%~fi" "000!_cnt!" "!_out_dir!"

4)您的函数:label %:^)将按数字顺序重命名文件,以便可以按日期顺序连接,并将文件移动到处理中的临时文件夹:

%:^)
...
set "_new=%~2" & call move "%~1" "%~3\!_new:~-4!.pdf"

5)在 pdf 文件中结束循环后,您的临时文件夹已经包含感兴趣的文件,因此运行pdftk以连接已保存/生成所需的文件:

pushd "Full\Path\To\your\pdfTK\Folder\bin"
pdftk.exe "!_out_dir!\*.pdf" cat output "!_results!"

6)当在执行/处理中到达该if行时,完成for /f循环后,您的蝙蝠将删除创建的文件夹并退出/关闭执行......

if not "%~nx1"=="" (...
     ) else (rmdir /s /q "!_out_dir!\." & popd & endlocal & goto :eOf)

其他资源:

相关内容