#author("2021-02-10T05:48:12+00:00","default:hotate","hotate") #author("2021-02-16T08:42:38+00:00","default:hotate","hotate") #contents &tag(Python,PDF); * 情報 [#v158b49b] ** 事例 [#sd105551] - [[ゼロからはじめるPython(69) Pythonを使って自動で申請書PDFに書き込もう(その1) | TECH+>https://news.mynavi.jp/article/zeropython-69/]] (2020/11/13) 今回はライブラリのインストールと簡単な図形の描画までの手順を紹介する。 - [[PythonでPDFファイルの文書情報の取得と書き換え — 某エンジニアのお仕事以外のメモ(分冊)>https://water2litter.net/rum/post/python_pypdf2_documentinfo/]] PyPDF2モジュールを使って、PDFファイルの文書情報(メタデータ)を取得したり書き換えたりしてみました。 * ライブラリ [#u2f9bc34] ** 比較 [#qb4d90cc] - [[【python】既存のPDFファイルにテキストを差し込む - Qiita>https://qiita.com/mototoke/items/7fc4c65305c4180521e4]] フォントは日本語対応できるものをちゃんと調べないと文字化けで駄目そう。 また、既存PDFの読み込みもファイルによってはフォント関係で駄目なのがあるみたいです。 (pdfrwで日本語フォントの入ったpdfが読み込めないのがいくつかありました) ** 画像挿入 [#nd5e148e] - [[Working with PDFs in Python: Adding Images and Watermarks>https://stackabuse.com/working-with-pdfs-in-python-adding-images-and-watermarks/]] In this article you will learn how add images to your PDF in the form of watermarks, stamps, and barcodes. ** ReportLab [#p60cf384] - [[Pythonのreportlabの使い方まとめ - Qiita>https://qiita.com/takahashi_you/items/8c5fb1f07db1825c67a5]] Pythonから帳票等でPDFファイルを出力する際に必要になりそうな書き方をまとめた記事です。 -- コメント:"page.showPage()"の追加が必要だった。(2021/02/05) - [[Pythonで日本語をPDFに出力する(ReportLabを利用) | ガンマソフト株式会社>https://gammasoft.jp/blog/pdf-japanese-font-by-python/]] 日本語にデフォルトで対応しており、HeiseiMin-W3とHeiseiKakuGo-W5がそのまま使えます。 -- コメント:フォントを指定すること無く、デフォルトで日本語を出力できた。(2021/02/05) - [[python + reportlab で 履歴書フォーマットPDFを作成 - Qiita>https://qiita.com/chiba___/items/c91ba97864efceb81f4d]] PythonのPDF作成ライブラリのreportlabを使用して履歴書フォーマット(JIS規格)を作成しました。 ** PyMuPDF [#h72fe97a] - [[Introduction — PyMuPDF 1.18.7 documentation>https://pymupdf.readthedocs.io/en/latest/intro.html]] PyMuPDF is a Python binding for MuPDF – “a lightweight PDF and XPS viewer”. - [[PyPDF2?いやPyMuPDFでしょ - Qiita>https://qiita.com/shin1007/items/d5391aa0ee14463b780c]] PyPDF2だと読み書きのときにエラーをはかれる。 *** [[トラブル]] [#kbe6eeb0] + 問題:画像の配置位置がずれる。(2021/02/08) -- 原因:[[PDF]]内の座標系が異なって設定されているため。 -- 参考#1: --- [[python 3.x - PyMuPDF insert image at bottom - Stack Overflow>https://stackoverflow.com/questions/60711402/pymupdf-insert-image-at-bottom]] Some pages internally change the standard PDF geometry (which uses the bottom-left page point as (0, 0)) to something else, and at the same time do not encapsulate this doing properly as they should. --- [[Collection of Recipes — PyMuPDF 1.18.8 documentation>https://pymupdf.readthedocs.io/en/latest/faq.html#misplaced-item-insertions-on-pdf-pages]] The creator of the PDF has established a non-standard page geometry without keeping it “local” (as they should!). Most commonly, the PDF standard point (0,0) at bottom-left has been changed to the top-left point. So top and bottom are reversed – causing your insertion to be misplaced. -- 対策#1:Page.cleanContents()を呼べば良い。但し、他のプロパティも削除されるので注意。 -- 参考#2: --- [[python - PyMuPDF | inserted image is in the wrong place of a pdf page - Stack Overflow>https://stackoverflow.com/questions/59179002/pymupdf-inserted-image-is-in-the-wrong-place-of-a-pdf-page]] The following snippet resets the geometry of the page: if not(page._isWrapped): page._wrapContents() --- [[Functions — PyMuPDF 1.18.8 documentation>https://pymupdf.readthedocs.io/en/latest/functions.html#Page.is_wrapped]] Page.is_wrapped Indicate whether Page.wrap_contents() may be required for object insertions in standard PDF geometry. -- 対策#2: --- 上記のコードをそのままで良い。他への影響は少ないらしい。 --- 上記のコードをそのままで良く、他への影響は少ないらしい。ただし、上記の対策#1で成功したPDFファイルの処理に成功しないことが有った。(2021/02/16) * 関連 [#pc9b4b07] #related #taglist(tag=PDF)