使用excel vba合并文件夹中的所有pdf文件
在一个文件夹中,我只有最初通过导出excel工作表创建的pdf文件。现在我想合并这些文件到一个,并通过电子邮件发送它 我知道如何在excel vba中通过电子邮件发送单个文档。但是我完全不知道如何做合并部分。有人能帮我吗 在互联网上搜索时,我遇到了不同的网站,例如,这些网站或多或少都在处理这个问题。但我真的不知道使用excel vba合并文件夹中的所有pdf文件,vba,excel,Vba,Excel,在一个文件夹中,我只有最初通过导出excel工作表创建的pdf文件。现在我想合并这些文件到一个,并通过电子邮件发送它 我知道如何在excel vba中通过电子邮件发送单个文档。但是我完全不知道如何做合并部分。有人能帮我吗 在互联网上搜索时,我遇到了不同的网站,例如,这些网站或多或少都在处理这个问题。但我真的不知道 了解这些代码…我几乎可以肯定,如果没有安装AdobeAcrobat,VBA将无法做到这一点。如果你有Acrobat,你可以试试这个 Sub MergePDFs() ' ZVI:2013
了解这些代码…我几乎可以肯定,如果没有安装AdobeAcrobat,VBA将无法做到这一点。如果你有Acrobat,你可以试试这个
Sub MergePDFs()
' ZVI:2013-08-27 http://www.vbaexpress.com/forum/showthread.php?47310-Need-code-to-merge-PDF-files-in-a-folder-using-adobe-acrobat-X
' Reference required: "VBE - Tools - References - Acrobat"
' --> Settings, change to suit
Const MyPath = "C:\Temp" ' Path where PDF files are stored
Const MyFiles = "1.pdf,2.pdf,3.pdf" ' List of PDFs to ne merged
Const DestFile = "MergedFile.pdf" ' The name of the merged file
' <-- End of settings
Dim a As Variant, i As Long, n As Long, ni As Long, p As String
Dim AcroApp As New Acrobat.AcroApp, PartDocs() As Acrobat.CAcroPDDoc
If Right(MyPath, 1) = "\" Then p = MyPath Else p = MyPath & "\"
a = Split(MyFiles, ",")
ReDim PartDocs(0 To UBound(a))
On Error GoTo exit_
If Len(Dir(p & DestFile)) Then Kill p & DestFile
For i = 0 To UBound(a)
' Check PDF file presence
If Dir(p & Trim(a(i))) = "" Then
MsgBox "File not found" & vbLf & p & a(i), vbExclamation, "Canceled"
Exit For
End If
' Open PDF document
Set PartDocs(i) = CreateObject("AcroExch.PDDoc")
PartDocs(i).Open p & Trim(a(i))
If i Then
' Merge PDF to PartDocs(0) document
ni = PartDocs(i).GetNumPages()
If Not PartDocs(0).InsertPages(n - 1, PartDocs(i), 0, ni, True) Then
MsgBox "Cannot insert pages of" & vbLf & p & a(i), vbExclamation, "Canceled"
End If
' Calc the number of pages in the merged document
n = n + ni
' Release the memory
PartDocs(i).Close
Set PartDocs(i) = Nothing
Else
' Calc the number of pages in PartDocs(0) document
n = PartDocs(0).GetNumPages()
End If
Next
If i > UBound(a) Then
' Save the merged document to DestFile
If Not PartDocs(0).Save(PDSaveFull, p & DestFile) Then
MsgBox "Cannot save the resulting document" & vbLf & p & DestFile, vbExclamation, "Canceled"
End If
End If
exit_:
' Inform about error/success
If Err Then
MsgBox Err.Description, vbCritical, "Error #" & Err.Number
ElseIf i > UBound(a) Then
MsgBox "The resulting file is created:" & vbLf & p & DestFile, vbInformation, "Done"
End If
' Release the memory
If Not PartDocs(0) Is Nothing Then PartDocs(0).Close
Set PartDocs(0) = Nothing
' Quit Acrobat application
AcroApp.Exit
Set AcroApp = Nothing
End Sub
不确定这是否是最好的解决方案,但有一个Adobe插件可供下载和使用,这篇文章中提到:如果可以,请使用Adobe acrobat。他们有一个合并功能。很好的建议。。。(请注意,它是Acrobat,而不是Acrobat Reader。)另请参见。此外,还有一些现有的答案可能会帮助您解决问题(无论您的问题是什么,因为您的问题并不清楚)。欢迎使用!这是一个程序员编写自己的代码并在试图自己解决某个特定问题后与之共享问题的网站。如果,之后,你有一个特定的问题,请分享你的帖子和一些背景信息。一些让您开始学习的好读物:“以及提示和建议。感谢大家的投入。我试着想出一个解决方案,一旦我有了,我会把它贴在这里。非常感谢!不幸的是,我没有AdobeAcrobat。因此,除了手动创建它,我别无选择。但您提供的代码肯定会起作用:)
import glob
from PyPDF2 import PdfFileWriter, PdfFileReader
def merger(output_path, input_paths):
pdf_writer = PdfFileWriter()
for path in input_paths:
pdf_reader = PdfFileReader(path)
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
with open(output_path, 'wb') as fh:
pdf_writer.write(fh)
if __name__ == '__main__':
paths = glob.glob('C:/your_path_here/*.pdf')
paths.sort()
merger('pdf_merger.pdf', paths)