3.项目:从多个PDF中合并选择页面

#! python 3

# combinePdfs.py - Combines all the PDFs in the current working directory into a string PDF.

import PyPDF2,os


# Get all the PDF fileNames

pdfFiles = []

for filenamein os.listdir('.'):

    if filename.endswith('.pdf'):

        pdfFiles.append(filename)

        pdfFiles.sort(key = str.lower)


pdfWriter = PyPDF2.PdfFileWriter()


# LOOP through all the PDF files

for filename in pdfFiles:

    pdfFileObj =open(filename, 'rb')

    pdfReader = PyPDF2.PdfFileReader(pdfFileObj)

    # LOOP through all the pages (except the first) and add them.

    for pageNum in range(1,pdfReader.numPages):

        pageObj = pdfReader.getPage(pageNum)

        pdfWriter.addPage(pageObj)


# Save the resulting PDF to a file.

pdfOutputFile =open('all.pdf','wb')

pdfWriter.write(pdfOutputFile)

pdfOutputFile.close()

遇到的问题:

1.Python中List的sort的用法:pdfFiles.sort(key = str.lower)  

2.Python中路径拼接:pdfFileObj =open(filename, 'rb')

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 选择君 作为一个嵌入式工程师,受够了C语言的各种“低级”。趁着潮流,想学学最近大火的python,期望来年...
    SelectMan阅读 3,725评论 0 1
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,164评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,487评论 0 23
  • 每年回家过年,打牌是免不了的事。 即便不是和朋友在外面玩,一家人也会聚在一起搓麻将。所以,免不了。 搓麻将我多半是...
    土川兄一终身建设阅读 3,790评论 0 0
  • 在学驾照的时候,我接触到了很多朋友,但印象最深刻的是一个看起来略微胖胖的,说话比较幽默的,爱笑的女孩子。我曾经是一...
    笔下华年阅读 3,564评论 2 6