Skip to content

Instantly share code, notes, and snippets.

@keisukefukuda

keisukefukuda/a_pdfmark.py

Last active Sep 17, 2019
Embed
What would you like to do?
Generate pdfmarks (TOC) file, to feed to gs command
# LICENSE: MIT License
# How to use:
# $ pip install PyYAML fire pdfrw
# $ python pdfmark.py --infile=pdfmarks.yaml --outfile=pdfmarks
# $ gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=out.pdf in.pdf
#
import sys
import pprint # NOQA
import yaml
import fire
import pdfrw
def generate(sec_list, base_page, out=sys.stdout, depth=0):
for sec in sec_list:
if 'sub' in sec:
out.write("[/Count {sign}{count} /Title {title} /Page {page} /OUT pdfmark\n".format(
sign=('-' if depth == 0 else '-'),
count=len(sec['sub']),
title=pdfrw.objects.pdfstring.PdfString.encode(sec['title']),
page=sec['page'] + base_page - 1))
generate(sec['sub'], base_page, out, depth+1)
else:
out.write("[/Title {title} /Page {page} /OUT pdfmark\n".format(
title=pdfrw.objects.pdfstring.PdfString.encode(sec['title']),
page=sec['page'] + base_page - 1))
def main(infile, outfile="-") -> None:
with open(infile, 'r') as f:
data = yaml.load(f)
pprint.pprint(data)
print('base_page = {}'.format(data['base_page']))
# Generate
if outfile == '-':
outfile = sys.stdout
else:
outfile = open(outfile, 'w')
generate(data['toc'], data['base_page'], out=outfile)
outfile.close()
if __name__ == '__main__':
fire.Fire(main)
# Table of Contents from 「明解演習 数理統計 (明解演習シリーズ)」 by 小寺 平治
# https://www.kyoritsu-pub.co.jp/bookdetail/9784320013810
base_page: 9
toc:
- title: "第1章 確率"
page: 1
sub:
- {"title": "順列 組み合わせ", "page": 1}
- {"title": "確率とその声質", "page": 2}
- {"title": "事象の独立性", "page": 5}
- title: "第2章 確率変数"
page: 26
sub:
- {"title": "2.1 確率変数 確率分布", "page": 26}
- {"title": "2.2 多次元分布", "page": 27}
- {"title": "2.3 確率変数の関数の分布", "page": 28}
- {"title": "2.4 平均 分散", "page": 29}
- {"title": "2.5 積率母関数", "page": 31}
- title: "第3章 確率変数"
page: 54
sub:
- {"title": "二項分布", "page": 54}
- {"title": "ポアソン分布", "page": 54}
- {"title": "負の二項分布", "page": 55}
- {"title": "幾何分布", "page": 55}
- {"title": "超幾何分布", "page": 56}
- {"title": "多項分布", "page": 56}
- {"title": "正規分布", "page": 57}
- {"title": "t分布", "page": 57}
- {"title": "指数分布", "page": 58}
- {"title": "ガンマ分布", "page": 58}
- {"title": "ワイブル分布", "page": 59}
- {"title": "カイ二乗分布", "page": 59}
- {"title": "一様分布", "page": 60}
- {"title": "ベータ分布", "page": 60}
- {"title": "F分布", "page": 61}
- {"title": "コーシー分", "page": 61}
- title: "第4章 統計記述と標本分布"
page: 78
sub:
- {"title": "4.1 資料の整理", "page": 78}
- {"title": "4.2 標本平均の分布", "page": 81}
- {"title": "4.3 正規母集団からの標本", "page": 82}
- {"title": "4.4 有限母集団からの標本", "page": 83}
- title: "第5章 適合度・独立性の検定"
page: 105
sub:
- {"title": "5.1 統計的仮説の検定", "page": 105}
- {"title": "5.2 適合度の検定", "page": 106}
- {"title": "5.3 正規確率紙", "page": 106}
- {"title": "5.4 独立性の検定", "page": 107}
- title: "第6章 点推定"
page: 116
sub:
- {"title": "6.1 最尤推定量", "page": 116}
- {"title": "6.2 望ましい推定量", "page": 117}
- title: "第7章 母数の検定と区間推定"
page: 127
sub:
- {"title": "7.1 母分散の検定と推定", "page": 105}
- {"title": "7.2 母平均の検定と推定", "page": 106}
- {"title": "7.3 等平均仮説の検定", "page": 106}
- {"title": "7.4 母比率の推定と検定", "page": 107}
- {"title": "7.5 母相関係数の検定と推定", "page": 107}
- title: "ゼミナールの解答"
page: 150
sub:
- {"title": "<第1章の解答>", "page": 150}
- {"title": "<第2章の解答>", "page": 155}
- {"title": "<第3章の解答>", "page": 165}
- {"title": "<第4章の解答>", "page": 169}
- {"title": "<第5章の解答>", "page": 175}
- {"title": "<第6章の解答>", "page": 179}
- {"title": "<第7章の解答>", "page": 182}
- title: "分布間の相互関係"
page: 187
- title: "基本確率分布"
page: 188
- title: "付表"
page: 190
sub:
- {"title": "ポアソン分布表", "page": 190}
- {"title": "標準正規分布の確率", "page": 192}
- {"title": "標準正規分布のパーセント点", "page": 193}
- {"title": "カイ二乗分布のパーセント点", "page": 194}
- {"title": "t分布のパーセント点", "page": 195}
- {"title": "F分布の10パーセント点", "page": 196}
- {"title": "F分布の5パーセント点", "page": 198}
- {"title": "F分布の2.5パーセント点", "page": 200}
- {"title": "F分布の1パーセント点", "page": 202}
- {"title": "F分布の0.5パーセント点", "page": 204}
- {"title": "z変換表1・z変換表2", "page": 206}
- {"title": "正規確率紙", "page": 207}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.