惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Blog — PlanetScale
Blog — PlanetScale
博客园 - Franky
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
D
Docker
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
H
Help Net Security
B
Blog
宝玉的分享
宝玉的分享

少数派

派早报:Google 发布 Fitbit Air 等 - 少数派 「新人报到」確認需求,再開始 - 少数派 从 SOLO 独立开发者社区,我看到了越来越多开发者开始做自己的产品 - 少数派 我怎么管理那些"不常做,但总会忘"的生活事项 - 少数派 人形机器人量产元年,数据才是具身智能的“生死线” - 少数派 BuhoLaunchpad 高度还原 Mac 启动台:开发历程与思考 - 少数派 五年陪伴依然不舍,DIY 换壳后让罗技 MX Master 3 继续服役 - 少数派 新玩意 240|少数派的编辑们最近买了啥? - 少数派 一日一技|为什么你应该关闭 iOS 的键盘声音 - 少数派 我做了个插件和 Skills,一键提取任何网站的设计规范 Design.md - 少数派 住在三四线城市的你,该开始录播客了 - 少数派 甘南秘境,大白高国 - 少数派 AI的审美:谁让把我变成川内倫子 - 少数派 返工怎能不烦恼,打工人片单总有一部是你的「嘴替」 - 少数派 为了让「上厕所」更健康,我做了一个小工具 - 少数派 AI + Skill,能够让生成的文章去除 AI 味吗? - 少数派 新玩意|韶音OpenDots ONE 耳夹式耳机 - 少数派 《美满》| 在每一个春天的晚上相爱(362) - 少数派 新玩意|优篮子 PS01 MagSnap 磁吸支架 - 少数派 自我整合手记 | 我开始早睡了:用稳定规则,为自由托底 - 少数派 用龙虾(OpenClaw)两个多月,我最深的12个体会 - 少数派 听歌时间到,12 张你可能错过的 2025 华语乐坛好专辑 - 少数派 承诺能追吗 - 少数派 macOS 26启动台没了? 我做了个不一样的App启动器 - Keboard - 少数派 《四海为家的人》| INTJ对话INTJ(361) - 少数派 你发过的那些黑历史,是时候一次清干净了 - 少数派 新玩意:安安静静玩,越玩越专注:计客密码机 - 少数派 iPad 用户首次体验 Android 平板:vivo Pad6 Pro - 少数派 数据逻辑强 - 少数派 极北行+ | 一路向北,探访日本至北之地 | 001 - 少数派
在 PDF 文件右侧加上做笔记用的大片空白 - 少数派
2017-02-08 · via 少数派

本来是在 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:

  1. Can’t load python modules installed via pip from site packages directory 
  2. Merge Translated Pages Using PyPDF2
  3. PyPDF2 Documentation
  4. Pythonista appex
  5. Pythonista os.path