I created all three PDFs and checked them. The one oddity is that the bullet characters in terms.pdf come out as (cid:127) when extracted.
Files (in the working folder, built by scripts/make.py, checked by scripts/check.py):
invoice.pdfis 1 page. It has a title for "Northwind Sample Co" and three line items, and everything is marked as sample.terms.pdfis 1 page. It has the heading "Sample Terms" and three bullet terms.combined.pdfis 2 pages, with the invoice first and the terms second. That matches 1 + 1 pages.
Text extracted from combined.pdf
- Page 1 has the invoice title, the "fictional / made-up sample data" note, the table rows, and the "SAMPLE - not a real invoice." footer.
- Page 2 has "Sample Terms" and the three terms: payment due in 30 days, 1% monthly late fee, and goods remaining sample property until paid. Each one is marked "(sample term)".
Table extracted from page 1 (one table found):
| Item | Qty | Unit Price | Amount |
|---|---|---|---|
| Sample Widget | 10 | $5.00 | $50.00 |
| Sample Gadget | 2 | $25.00 | $50.00 |
| Sample Service Hour | 4 | $30.00 | $120.00 |
| Total | $220.00 |
Verification: The line items add up: 50 + 50 + 120 = 220, which matches the stated total. The extracted table has the same 5 rows and 4 columns I built, with no merged or missing cells.
Problem: pdfplumber reads the bullet glyph as (cid:127) instead of "•". That is how the built-in Helvetica font encodes the bullet, so it is probably an extraction quirk and the bullets should display normally. I haven't rendered the pages, so I can't confirm how they look. If clean extracted text matters to you, I can switch to a plain "-" marker.
Test run: two sample one-page PDFs built with reportlab, merged with pypdf, then read back with pdfplumber, in a temporary folder. The run was allowed to write files and run python3 only. I re-opened the files myself: 1 + 1 pages merged into 2, and the table text matched. The model flagged that the bullet glyph extracts as (cid:127). Pages were not rendered, so appearance was not checked. All content is sample.
できること
3つの定番ライブラリを使います。構造(結合、分割、並べ替え、回転、フォーム、メタデータ)は pypdf、テキストと表は pdfplumber、新しい PDF の生成は reportlab です。まず各入力を調べ(ページ数、サイズ、暗号化、フォーム項目、各ページから抽出できる文字数。スキャンページを早く見つけられます)、作業を実行し、各出力を開き直してページ数、テキストの見本、フォームの値、ファイルサイズを確認します。抽出できる文字がないページは OCR が必要と伝え、抽出した表が信頼できないかもしれないときは注意します。保護された PDF は、使う権利のあるファイルについてあなたがパスワードを渡したときだけ開き、保護の解除は試みません。
出力
新しい PDF ファイルと作成に使ったスクリプト、変更点、確認結果、スキャンページやフォント不足などの問題点。
こんな場面に
文書の結合と分割、報告書からの表の抽出、透かしとページ番号の追加、フォーム入力、簡単な請求書やレポートの生成。
中リスク:指示のみのファイルで自前のスクリプトはありませんが、Python を書いて実行し、作業フォルダーにファイルを作り、仮想環境への pypdf、pdfplumber、reportlab のインストールを勧めることがあります。元のファイルは上書きせず、パスワード保護の解除もしません。一時フォルダーでサンプルの PDF を使って一度試用し、2つの PDF を作って結合し、表を正しく抽出しました。箇条書きの記号は抽出時に (cid:127) になり、ページは描画していないため見た目は確認していません。AIBars 制作(MIT)。