在 PDF 文件右侧加上做笔记用的大片空白 您所在的位置:网站首页 怎么给pdf加空白页面边框 在 PDF 文件右侧加上做笔记用的大片空白

在 PDF 文件右侧加上做笔记用的大片空白

2024-07-09 08:50| 来源: 网络整理| 查看: 265

本来是在 blog 上写的,好像写 sspai 征文的时候立 flag 表示会把这个写一遍那就买一送一吧。

需求是给 PDF 文件的每一页右边加上任意大小的空白,效果如下:

Screen Shot 2017-02-08 at 6.26.17 PM.png

图中 PDF 文件的右侧的空白是我另加上去的,方便 Apple Pencil 用户和其他有需求的朋友们在空白处做笔记,打草稿。

Python 代码

实现的方式是用 Python 的一个叫 PyPDF2 的 Module。这个在 macOS 上内置的 Python 里并没有,需要额外下载。这个 Module 里 PDF page 的 class 是PageObject。他包含一个 叫mergeTranslatedPage 的 method,能够让把两个 page 合并在一起。所以只要新建一个空白页,然后把空白页平移一定的距离之后在合并在一起,就可以实现这个功能了。

PyPDF2 不包含在 macOS 自带的 Python 里,要用 pip 安装一下。但是假如要在 Automator 里用 Python Shell Script 的话,要用 /usr/bin/python 的 Python 才行(假如你的 mac 上有很多 Python 的话)。所以在用 pip 安装的时候记得用:/usr/bin/python -m pip install PyPDF2。

下面是 Python 代码:

from PyPDF2 import PdfFileReader, PdfFileWriter filepath = #Input PDF File Path outpath = #Output PDF File Path infile = PdfFileReader(filepath,'rb') outfile = PdfFileWriter() tempfile = PdfFileWriter() tempfile.addPage(infile.getPage(0)) tempfile.addBlankPage() blankPage = tempfile.getPage(1) for i in range(infile.getNumPages()): p = infile.getPage(i) p.mergeTranslatedPage(blankPage,300,0,expand=True) #300 is the width of the margin outfile.addPage(p) with open(outpath,'wb') as f: outfile.write(f)

我在 macOS 上用 CodeRunner 测试了这段代码,没什么毛病。但是我一开始是在 iPad Pro 上用 Pythonista 上写的,PDF 文件的页数如果不多的话没什么问题,一旦页数过多,Pythonista 就会报错;同时 PDF 里的公式可能会有显示不正常的情况。所以还是推荐在 macOS 上用。但是我还是写一下怎么在 Pythonista 上用。

Pythonista from PyPDF2 import PdfFileReader, PdfFileWriter import appex import os filepath = appex.get_file_path() infile = PdfFileReader(filepath,'rb') outfile = PdfFileWriter() tempfile = PdfFileWriter() tempfile.addPage(infile.getPage(0)) tempfile.addBlankPage() blankPage = tempfile.getPage(1) for i in range(0,45): p = infile.getPage(i) p.mergeTranslatedPage(blankPage,300,0,expand=True) outfile.addPage(p) with open(os.path.expanduser("~/Documents/PDFProcess/output.pdf"),'wb') as f: outfile.write(f)

appex 是和 iOS app extension 有关的 module. appex.get_file_path() 是一个返回文件 iOS 下文件地址的 method。 os.path.expanduser("~\Documents") 把 "~\Documents"( "~"表示 Pythonista 的根目录)转化成 iOS 下实际的目录。这个其实是在 app extension 里用的,在 Pythonista 里保存这段 Script。然后在你想改的 PDF 里打开 app extension,选 Run Pythonista Script,然后在 All Scripts 里找到这个 script 跑就行。

Automator

用下面这一段 Python Shell Script 可以利用 Automator 做一个 app:

import sys from PyPDF2 import PdfFileReader, PdfFileWriter for f in sys.argv[1:]: filepath = f.decode('utf8') infile = PdfFileReader(filepath,'rb') outfile = PdfFileWriter() tempfile = PdfFileWriter() tempfile.addPage(infile.getPage(0)) tempfile.addBlankPage() blankPage = tempfile.getPage(1) for i in range(infile.getNumPages()): p = infile.getPage(i) p.mergeTranslatedPage(blankPage,300,0,expand=True) outfile.addPage(p) with open(filepath,'wb') as f: outfile.write(f)

Automator 的设置截个图在下面。注意 Shell 用 /usr/bin/python 和 Pass Input 用 as arguments。

Screen Shot 2017-02-08 at 6.26.44 PM.png

Reference:

Can’t load python modules installed via pip from site packages directory Merge Translated Pages Using PyPDF2PyPDF2 DocumentationPythonista appexPythonista os.path


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有