부반응 의심 보고 상황에 대한 PDF를 CSV로 변환
                                            
                                                
                                                
                                                
                                                
                                                
                                                 7019 단어  파이썬PDF 변환pdfplumber
                    
htps //w w. mhlw. . jp / 혼텐 t / 10601000 / 000853766. pdf
pdfplumber에서 테이블을 추출하려고하면 테이블 끝에 선이 없으므로 마지막 행을 얻을 수 없습니다.

세로선 아래의 위치를 가져와 최종 수평선 추가

세로선 추가

테이블 확인

PDF 다운로드
!wget https://www.mhlw.go.jp/content/10601000/000853766.pdf -O data.pdf
 프로그램
import pdfplumber
import pandas as pd
from tqdm.notebook import tqdm
with pdfplumber.open("data.pdf") as pdf:
    dfs = []
    for page in tqdm(pdf.pages[1:]):
        # 縦線
        vertical = [
            edge["x0"]
            for edge in page.debug_tablefinder().edges
            if edge["orientation"] == "v"
        ]
        # 縦線の一番下の位置
        bottom = max(
            [
                edge["bottom"]
                for edge in page.debug_tablefinder().edges
                if edge["orientation"] == "v"
            ]
        )
        # 横線
        horizontal = [
            edge["top"]
            for edge in page.debug_tablefinder().edges
            if edge["orientation"] == "h"
        ]
        # 横線に縦線の一番下の位置を追加
        horizontal.append(bottom)
        table_settings = {
            "vertical_strategy": "explicit",
            "explicit_vertical_lines": vertical,
            "horizontal_strategy": "explicit",
            "explicit_horizontal_lines": horizontal,
            "snap_tolerance": 3,
            "intersection_tolerance": 3,
        }
        table = page.extract_table(table_settings)
        df_tmp = pd.DataFrame(table)
        dfs.append(df_tmp)
df = pd.concat(dfs)
df.shape
df.to_csv("result.csv", encoding="utf_8_sig")
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(부반응 의심 보고 상황에 대한 PDF를 CSV로 변환), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/barobaro/items/af9ad5485566320c5226
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                 우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
!wget https://www.mhlw.go.jp/content/10601000/000853766.pdf -O data.pdf
import pdfplumber
import pandas as pd
from tqdm.notebook import tqdm
with pdfplumber.open("data.pdf") as pdf:
    dfs = []
    for page in tqdm(pdf.pages[1:]):
        # 縦線
        vertical = [
            edge["x0"]
            for edge in page.debug_tablefinder().edges
            if edge["orientation"] == "v"
        ]
        # 縦線の一番下の位置
        bottom = max(
            [
                edge["bottom"]
                for edge in page.debug_tablefinder().edges
                if edge["orientation"] == "v"
            ]
        )
        # 横線
        horizontal = [
            edge["top"]
            for edge in page.debug_tablefinder().edges
            if edge["orientation"] == "h"
        ]
        # 横線に縦線の一番下の位置を追加
        horizontal.append(bottom)
        table_settings = {
            "vertical_strategy": "explicit",
            "explicit_vertical_lines": vertical,
            "horizontal_strategy": "explicit",
            "explicit_horizontal_lines": horizontal,
            "snap_tolerance": 3,
            "intersection_tolerance": 3,
        }
        table = page.extract_table(table_settings)
        df_tmp = pd.DataFrame(table)
        dfs.append(df_tmp)
df = pd.concat(dfs)
df.shape
df.to_csv("result.csv", encoding="utf_8_sig")
Reference
이 문제에 관하여(부반응 의심 보고 상황에 대한 PDF를 CSV로 변환), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/barobaro/items/af9ad5485566320c5226텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)