excel 如何使用VBA从PDF中删除任何页面?

piztneat  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(389)

我试图使用VBA从PDF文档中删除一个空白页,但出现了一些问题...

> Sub deletepage()

A as acrobat.acroapp

B as acrobat.acropddoc

Set a = createobject("acroexch.app")

Set b = createobject("acroexch.pddoc")

A.show

Myfoc.open(filename)

If mydoc.deletepages(1,2)=True then

Deleted

Else

Not deleted

End if

> End sub
bvjxkvbb

bvjxkvbb1#

如果您不介意将Python与VBA结合使用,可以尝试以下操作:

Sub DeletePageFromPDF()
    Dim RetVal As Long
    ' Call the Python script to delete the 3rd page from the PDF file
    RetVal = Shell("python.exe C:\python\delete_page.py C:\TEST\test.pdf 3", vbNormalFocus)
End Sub

Python脚本'delete_page. py':

import sys
from PyPDF2 import PdfReader, PdfWriter

def delete_page(file_path, page_number):
    pdf = PdfReader(file_path)
    #pdf.getPage(page_number).expand_objects()
    #pdf.getPage[page_number]
    pdf_writer = PdfWriter()

    # Loop through all pages in the PDF file
    for i in range(len(pdf.pages)):
        # Skip the page to be deleted
        if i != page_number - 1:
            pdf_writer.add_page(pdf.pages[i])

    # Write the updated PDF file
    with open(file_path, 'wb') as f:
        pdf_writer.write(f)

if __name__ == "__main__":
    file_path = sys.argv[1]
    page_number = int(sys.argv[2])
    delete_page(file_path, page_number)

但是你需要在你的系统上安装Python,我自己也刚刚开始使用Python,发现它是一个很好的"扩展",可以处理在VBA中变得复杂的事情。
试试看。
问候

相关问题