Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ Windows/macOS/Ubuntu:
+ 目录文本:将目录文本粘贴到“目录文本”框中。[如何获取目录文本](#获取目录文本)。对于已有目录页的文字版或扫描版 PDF,也可以点击“自动读取目录”从 PDF 前部目录页中识别并填充目录文本。
+ 编辑写入目录(可选项):根据目录文本自动生成的实际写入目录,可双击任一目录或页数进行编辑。同时支持拖动改变顺序/目录上下级关系。
+ 编辑页差(可选项):当目录中的标注页码与 PDF 实际页数不一致时,可在“页差”中填写差值,程序会在预览中换算出实际页数。也可以点击“自动填充页差”根据目录标题在 PDF 中的位置自动推断页差;文字版 PDF 可直接使用,扫描版 PDF 需要安装 OCR 可选依赖。
+ 写入:点击右下角的“写入”按钮,稍等片刻,待状态栏提示"******* Finished!"表示写入成功,此时可在pdf目录下找到包含书签的 *原文件名\_new.pdf* 文件。
+ 阅读器页码(可选项):默认保留原 PDF 已有的页码标签。若正文第 1 页对应 PDF 第 31 页,可选择“前置页罗马数字,正文从 1 开始”,程序会给前 30 页设置 `i…xxx`,第 31 页起设置 `1、2、3…`。起始页默认依据“页差 + 1”填写,也可关闭“根据页差”手动指定;手动指定不会改变书签跳转页。此选项会替换原 PDF 的已有页码标签。
+ 写入:点击右下角的“写入”按钮,待提示成功后,可在 PDF 目录下找到同时包含书签和所选页码标签的 *原文件名\_new.pdf* 文件。已有同名输出文件时会询问是否替换。

### 获取目录文本

Expand Down Expand Up @@ -72,7 +73,7 @@ Windows/macOS/Ubuntu:

运行源码所需环境:

+ Python2/3 均可,推荐Python3
+ Python 3.7 或更新版本
+ PyQt5
+ PyPDF
+ six
Expand Down Expand Up @@ -120,26 +121,13 @@ https://www.python.org/downloads/
通过cli运行接口支持最多6级目录, 目录文本通过文件输入更加容易编辑.

```
python run_cli.py --help myrepo/pdfdir
usage: run_cli.py [-h] [--offset OFFSET] [--l0 L0] [--l1 L1] [--l2 L2] [--l3 L3] [--l4 L4] [--l5 L5] pdfPath tocPath

Add content to PDF.

positional arguments:
pdfPath path of PDF
tocPath path of contents file

options:
-h, --help show this help message and exit
--offset OFFSET Page offset of contents
--l0 L0 Regular expression of level 0 of content
--l1 L1 Regular expression of level 1 of content
--l2 L2 Regular expression of level 2 of content
--l3 L3 Regular expression of level 3 of content
--l4 L4 Regular expression of level 4 of content
--l5 L5 Regular expression of level 5 of content
python run_cli.py book.pdf toc.txt --offset 30 --page-labels roman-body
# 正文起始页与页差不同,也可明确指定:
python run_cli.py book.pdf toc.txt --offset 30 --page-labels roman-body --body-start-page 31
```

默认 `--page-labels preserve` 会保留原文件页码标签;`roman-body` 会写入罗马数字前置页和从 1 开始的正文页码。运行 `python run_cli.py --help` 可查看其余目录层级参数。CLI 若已有同名输出文件会替换它。

### 打包源码

如果你想在本机打包此程序:
Expand Down
28 changes: 25 additions & 3 deletions run_cli.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import argparse

from src.pdfdirectory import add_directory
from src.pdf.page_labels import PageLabelPlan

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Add content to PDF.")
parser.add_argument("pdfPath", type=str, help="path of PDF")
parser.add_argument("tocPath", type=str, help="path of contents file")
parser.add_argument("--offset", type=int, default=0, help="Page offset of contents")
parser.add_argument(
"--page-labels", choices=("preserve", "roman-body"), default="preserve",
help="Preserve source page labels, or number front matter i, ii... and body 1, 2...",
)
parser.add_argument(
"--body-start-page", type=int, default=None,
help="One-based physical PDF page where body page 1 starts; defaults to offset + 1",
)
parser.add_argument(
"--l0",
type=str,
Expand Down Expand Up @@ -48,11 +57,24 @@
pdfPath = args.pdfPath
tocPath = args.tocPath
offset = args.offset
if args.page_labels == "preserve" and args.body_start_page is not None:
parser.error("--body-start-page requires --page-labels roman-body")
if args.page_labels == "roman-body":
body_start = args.body_start_page if args.body_start_page is not None else offset + 1
if body_start < 1:
parser.error("body start page must be at least 1; pass --body-start-page")
Comment thread
chroming marked this conversation as resolved.
label_plan = PageLabelPlan("roman-body", body_start)
else:
label_plan = PageLabelPlan()

# -- load toc
f = open(tocPath)
toc = f.read()
f.close()
add_directory(
toc, offset, pdfPath, args.l0, args.l1, args.l2, args.l3, args.l4, args.l5
)
try:
add_directory(
toc, offset, pdfPath, args.l0, args.l1, args.l2, args.l3, args.l4, args.l5,
page_label_plan=label_plan,
)
except ValueError as exc:
parser.error(str(exc))
Binary file modified src/gui/en.qm
Binary file not shown.
27 changes: 27 additions & 0 deletions src/gui/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,31 @@
<translation>Write directory</translation>
</message>
</context>
<context>
<name>Main</name>
<message><source>阅读器页码</source><translation>Reader page numbers</translation></message>
<message><source>保留原文件页码</source><translation>Preserve source labels</translation></message>
<message><source>前置页罗马,正文从 1 开始</source><translation>Roman front, body from 1</translation></message>
<message><source>根据页差</source><translation>Use page offset</translation></message>
<message><source>PDF 第 </source><translation>PDF page </translation></message>
<message><source> 页</source><translation> (physical)</translation></message>
<message><source>导出时保留原 PDF 的页码规则</source><translation>Keep the source PDF page labels</translation></message>
<message><source>页差不能推导正文起始页;取消勾选「根据页差」后手动指定</source><translation>Uncheck Use page offset, then set body start manually</translation></message>
<message><source>正文起始页超过 PDF 总页数</source><translation>Body start exceeds the PDF page count</translation></message>
<message><source>PDF 第 1–{} 页:i…;第 {} 页起:1…</source><translation>PDF pages 1–{}: i…; page {} onward: 1…</translation></message>
<message><source>PDF 第 1 页起:1…</source><translation>PDF page 1 onward: 1…</translation></message>
<message><source>;将替换原有页码规则</source><translation>; replaces source labels</translation></message>
<message><source>无法读取 PDF 页数</source><translation>Could not read PDF page count</translation></message>
<message><source>未导出的目录修改</source><translation>Unsaved directory edits</translation></message>
<message><source>打开其他文件前,如何处理当前修改?</source><translation>What should happen to the current edits?</translation></message>
<message><source>导出当前 PDF</source><translation>Export current PDF</translation></message>
<message><source>放弃修改</source><translation>Discard edits</translation></message>
<message><source>取消</source><translation>Cancel</translation></message>
<message><source>替换导出的 PDF</source><translation>Replace exported PDF</translation></message>
<message><source>替换已有文件?&#10;{}</source><translation>Replace the existing file?&#10;{}</translation></message>
<message><source>替换</source><translation>Replace</translation></message>
<message><source>正在写入 PDF…</source><translation>Writing PDF...</translation></message>
<message><source>已导出:</source><translation>Exported: </translation></message>
<message><source>导出失败:</source><translation>Export failed: </translation></message>
</context>
</TS>
Loading
Loading