python 使用PyPDF2仅选择PDF的第一页

q9rjltbz  于 2023-11-16  发布在  Python
关注(0)|答案(4)|浏览(142)

我试图剥离出只有第一页的多个PDF文件和合并到一个文件。(我每天收到150个PDF文件,第一页是我需要的发票,以下3至12页只是备份,我不需要)因此,输入是150个不同大小的PDF文件,我想要的输出是1个PDF文件,其中只包含150个文件中每个文件的第一页。
我所做的似乎是合并了所有的页面,除了第一页(这是我唯一需要的)。

# Get all PDF documents in current directory
import os

pdf_files = []
for filename in os.listdir("."):
    if filename.endswith(".pdf"):
        pdf_files.append(filename)
pdf_files.sort(key=str.lower)

# Take first page from each PDF
from PyPDF2 import PdfFileWriter, PdfFileReader

for filename in pdf_files:
    reader = PdfFileReader(filename)

writer = PdfFileWriter()
for pageNum in range(1, reader.numPages):
    page = reader.getPage(pageNum)
    writer.addPage(page)

with open("CombinedFirstPages.pdf", "wb") as fp:
    writer.write(fp)

字符串

yduiuuwa

yduiuuwa1#

试试这个:

# Get all PDF documents in current directory
import os

your_target_folder = "."
pdf_files = []
for dirpath, _, filenames in os.walk(your_target_folder):
    for items in filenames:
        file_full_path = os.path.abspath(os.path.join(dirpath, items))
        if file_full_path.lower().endswith(".pdf"):
            pdf_files.append(file_full_path)
pdf_files.sort(key=str.lower)

# Take first page from each PDF
from PyPDF2 import PdfFileReader, PdfFileWriter

writer = PdfFileWriter()

for file_path in pdf_files:
    reader = PdfFileReader(file_path)
    page = reader.getPage(0)
    writer.addPage(page)

with open("CombinedFirstPages.pdf", "wb") as output:
    writer.write(output)

字符串

t2a7ltrp

t2a7ltrp2#

做了一些修改。下面的代码对我来说是有效的。

import os
from PyPDF2 import PdfWriter, PdfReader

pdf_files = []
# Get all PDF documents in current directory
for filename in os.listdir("."):
    if filename.endswith(".pdf"):
        pdf_files.append(filename)
pdf_files.sort(key=str.lower)

# Take first page from each PDF    

pdf_writer = PdfWriter()

for filename in pdf_files:
    reader = PdfReader(filename)
    page = reader.pages[0]
    pdf_writer.add_page(page)

with open("CombinedFirstPages.pdf", "wb") as fp:
    pdf_writer.write(fp)

字符串

nfzehxib

nfzehxib3#

如果PDF没有文本,只包含图像,则其他答案不起作用。下面的答案适用于任何类型的PDF(相关pypdf doc

from pypdf import PdfWriter
from pathlib import Path

pdf_files = sorted(Path("<path-to-folder-with-files>").glob('**/*.pdf'))

# Take first page from each PDF

pdf_writer = PdfWriter()

for file in pdf_files:
    pdf_writer.append(file, pages=(0, 1))

with open("CombinedFirstPages.pdf", "wb") as fp:
    pdf_writer.write(fp)

字符串

lokaqttq

lokaqttq4#

此脚本获取所有PDF文件并将第一页转换为png。在当前执行目录中

#pip install pdf2image
import os
import tempfile
from pdf2image import convert_from_path
output_folder=os.getcwd() #current work directory

def pdf_to_png(pdf_name,source,destino):
    with tempfile.TemporaryDirectory() as path:
            images_from_path = convert_from_path(pdf_path=source+"/"+pdf_name,
            dpi=100,
            output_folder=destino,
            fmt="png",
            output_file=pdf_name[:-4],
            single_file=True)
            
for filename in os.listdir(output_folder):
    if filename.endswith(".pdf"):
        pdf_to_png(filename,output_folder,output_folder)

print("ok!")

字符串

相关问题