点院长 发表于 2020-4-10 18:07

入门小白求助Python有关编辑pdf目录问题

想给pdf加一个目录,在网上看见一个大神写的代码
书签txt和文件已准备好,想问问在程序代码中哪里添加pdf和书签文件的路径?
import re
import sys

from distutils.version import LooseVersion
from os.path import exists, splitext
from PyPDF2 import PdfFileReader, PdfFileWriter


is_python2 = LooseVersion(sys.version) < '3'


def _get_parent_bookmark(current_indent, history_indent, bookmarks):
    '''The parent of A is the nearest bookmark whose indent is smaller than A's
    '''
    assert len(history_indent) == len(bookmarks)
    if current_indent == 0:
      return None
    for i in range(len(history_indent) - 1, -1, -1):
      # len(history_indent) - 1   ===>   0
      if history_indent < current_indent:
            return bookmarks
    return None

def addBookmark(pdf_path, bookmark_txt_path, page_offset):
    if not exists(pdf_path):
      return "Error: No such file: {}".format(pdf_path)
    if not exists(bookmark_txt_path):
      return "Error: No such file: {}".format(bookmark_txt_path)

    with open(bookmark_txt_path, 'r') as f:
      bookmark_lines = f.readlines()
    reader = PdfFileReader(pdf_path)
    writer = PdfFileWriter()
    writer.cloneDocumentFromReader(reader)

    maxPages = reader.getNumPages()
    bookmarks, history_indent = [], []
    # decide the level of each bookmark according to the relative indent size in each line
    #   no indent:          level 1
    #   small indent:   level 2
    #       larger indent:level 3
    #   ...
    for line in bookmark_lines:
      line2 = re.split(r'\s+', unicode(line.strip(), 'utf-8')) if is_python2 else re.split(r'\s+', line.strip())
      if len(line2) == 1:
            continue

      indent_size = len(line) - len(line.lstrip())
      parent = _get_parent_bookmark(indent_size, history_indent, bookmarks)
      history_indent.append(indent_size)

      title, page = ' '.join(line2[:-1]), int(line2[-1]) - 1
      if page + page_offset >= maxPages:
            return "Error: page index out of range: %d >= %d" % (page + page_offset, maxPages)
      new_bookmark = writer.addBookmark(title, page + page_offset, parent=parent)
      bookmarks.append(new_bookmark)

    out_path = splitext(pdf_path)[0] + '-new.pdf'
    with open(out_path,'wb') as f:
      writer.write(f)

    return "The bookmarks have been added to %s" % pdf_path

if __name__ == "__main__":
    import sys
    args = sys.argv
    if len(args) != 4:
      print("Usage: %s " % args[0])
    print(addBookmark(args[1], args[2], int(args[3])))

点院长 发表于 2020-4-10 18:26

求助一下

Zeaf 发表于 2020-4-10 20:12

本帖最后由 Zeaf 于 2020-4-10 20:14 编辑

看with open后面吧,wb/r前面就是文件名

pjsaikou 发表于 2020-4-10 22:19

最前面加这两行
pdf_path = pdf路径
bookmark_txt_path = 书签txt路径

ymhld 发表于 2020-4-11 18:05

(addBookmark(args, args, int(args)


pDF文件 ,水印文件,偏移量
页: [1]
查看完整版本: 入门小白求助Python有关编辑pdf目录问题