jsvine / pdfplumber

Plumb a PDF for detailed information about each char, rectangle, line, et cetera — and easily extract text and tables.
MIT License
6.57k stars 659 forks source link

cannot extract text properly from a cropped pdf #308

Closed leiyuwork closed 3 years ago

leiyuwork commented 3 years ago

Thank you so muck for the fantastic job! I came across one issue while using pdfplumber to extract some texts from a cropped pdf. Initial pdf is here. 2020年版 (1332) 日本水産(株) .pdf It has two columns. I cropped pdf to extract text from each part separately, and the cropped pdf is here. 1111.pdf The code is import pdfplumber with pdfplumber.open("1111.pdf") as pdf: first_page = pdf.pages[0] print(first_page.extract_text()) and the result turned out to be get first_page for twice, and texts in page 1 and 2 are mixed.

samkit-jain commented 3 years ago

Hi @leiyuwork Appreciate your interest in the library. I don't think the PDF cropping was successful. Visually the crop seems fine but there are characters that extend outside the cropped page's actual width. There are a couple of things that you can do

  1. On the cropped 1111.pdf, for every page, run page = page.crop((0, 0, page.width, page.height)). It will ensure that objects that are within the bounds of the PDF's width and height are kept only. Here is the text output of page 1 when doing this
    1332
    にっ ぽん すい さん 【
    日 本 水 産
    【
    【
    【
    【本社】105–8676 東京都港区西新橋1-3-1   【TEL】03–6206–7000【
    【設立】1943.3 【上場】1949.5 【決算期】3月
    【特色】水産大手。冷食など食品比重大。国内・南米で養殖。EPAなど化成品でも
    実力。傘下に日水製薬
     CSR評価 
    人材活用 環境 企業統治 社会性 【
    AA AA AAA AA
    78.8 84.0 95.8 80.5 【
    【
     CSR基本評価  【
    基本 【
    AAA
    73.3
    【
     財務評価  【
    成長性 収益性 安全性 規模 【
    【
    A A B AAA
    74.2 76.4 66.5 80.8 【
    【
    【
    【経営理念】有 【
    【活動のマテリアリティ設定】有
    ①豊かな海を守り持続可能な水産資源の利用と調達を推進する    ②安全・安心で健
    康的な生活に貢献する  ③社会課題に取り組む多様な人材が活躍できる企業を目指
    す
    【方針の文書化】有
    (注)Webと社会・環境報告書にCSR方針を掲載している
    【活動の報告】紙とWeb
    【第三者の関与】無 【
    【英文の報告書】有 【
    【統合報告書】作成予定
    【ステークホルダー・エンゲージメント】有    内容報告:行っている 【
    水産資源、水産研究分野から2人の有識者とともに、ニッスイグループ取り扱い水産
    資源調査結果とIUU漁業への対応について意見交換を行った(18年8月)
    【汚職・贈収賄防止】方針有 【
    ①私たちは、高い倫理観を持ち、社会規範にのっとって行動し、より信頼される企業【
    を目指す(CSR行動宣言より) ②具体的な請託内容の有無を問わず、一切の贈賄【
    行為は行わない(倫理行動基準より)
    【ISO26000】活用
    【CSR部署】(専任)CSR部
    【
    【CSR担当役員】兼任有(取締役常務執行役員CSR担当(1人)、執行役員CSR部
    【
    管掌(1人))
    【同・CSR業務比率】半分以下
    (注)兼任役員2人体制としている
    【NPO・NGO連携】有(主な連携先:(公社)日本環境教育フォーラム、セカンドハーベ【
    スト・ジャパン、WWFジャパン、MSC日本、自然保護協議会、おやじ日本、WFP、荒
    川クリーンエイド・フォーラム、藤前干潟を守る会、ふくおかFUN)
    ①荒川クリーンエイド・フォーラムと連携し、海洋ゴミや河川ゴミに着目したマイクロプラ【
    スチック問題を取り上げ、従業員の環境意識向上を目的とした学習会を継続的に実
    施している   ②年間を通じ、セカンドハーベスト・ジャパンへ冷凍食品を寄贈している   ③
    【
    WFP主催の参加人数に応じて飢餓に苦しむ国の子どもへ給食が提供されるイベント
    の協賛企業となり、社員と家族がイベントに参加
    【CSR関連基準】国連GC、OECD多国籍企業GL、GRI-GL、ISO26000、日本経団
    【
    連:企業行動憲章、ILO中核的労働基準、国際統合報告FW、SDGs
    【
    【CSR関連・表彰歴】
    【
    (18年度)①食品産業優良企業等表彰CSR部門農林水産大臣賞((一財)食品産
    業センター)  ②ロングセラー平成貢献賞(日本食糧新聞社。大きな大きなやきおにぎ
    り)
    【
    【ESG情報の開示】開示
    【機関投資家・ESG調査機関等との対話】行っている(対話内容:取締役会の実効
    性評価などガバナンスや、SDGsに沿った成長シナリオ、ESG情報開示のあり方等)
    【
    【SRIインデックス等への組み入れ】SNAMサステナビリティ・インデックス
    【相談役・顧問制度】有
    相談役:1人  顧問:―
    導入目的:現経営陣へのアドバイス/対外活動
    メリット:代表取締役経験者から選任し(非常勤)、委任内容は「業務遂行に必要な
    事項のアドバイス、業界団体等の役職を含む渉外業務」。経営に直接関与すること【
    はなく、後者をメインにサポートしている 【
    【社外取締役による経営者評価】行っている 【
    【指名・報酬委員会等の設置】設置している 【
    【
    【
    【水産・農林業】
  2. pdfplumber natively supports .crop() functionality and you can use that on your original PDF.
leiyuwork commented 3 years ago

@samkit-jain Thank you so much! I realized the PDF cropping was not so successful and as you advised, I have used pdfplumber directly to crop the pdf and read. It worked well!