Skip to content

Repository files navigation

DePDF

An ultimate pdf file disintegration tool. DePDF is designed to extract tables and paragraphs into structured markup language [eg. html] from embedding pdf pages. You can also use it to convert pdf page or pdf to html.

Built on top of pdfplumber

Checkout automatically generated documentation if you wish.

Table of Contents

[toc]

Installation

pip install depdf

Example

fromdepdfimportDePDFfromdepdfimportDePage# generalwithDePDF.load('test/test.pdf') aspdfpdf_html=pdf.to_htmlprint(pdf_html)
# with dedicated configurationsc=Config(
debug_flag=True,
verbose_flag=True,
add_line_flag=True
)
pdf=DePDF.load('test/test.pdf', config=c)
page_index=23# start from zeropage=pdf_file.pages[page_index]
page_soup=page.soupprint(page_soup.text)

APIs

functionsusage
extract_page_paragraphsextract paragraphs from specific page
extract_page_tablesextract tables from specific page
convert_pdf_to_htmlconvert the entire pdf to html
convert_page_to_htmlconvert specific page to html

In-Depth

In-page elements

  • Paragraph
    • Text
    • Span
  • Table
    • Cell
  • Image

Common properties

property & methodexplanation
htmlconverted html string
soupconverted beautiful soup
bboxbounding box region
save_htmlwrite html tag to local file

DePDF HTML structure

<divclass="{pdf_class}">
%for <!--page-{pid}--><divid="page-{pid}" class="{page_class}">
%for {in_page_elements} endfor%
</div>
endfor%
</div>

DePage HTML element structure

Paragraph

<p>
{text-content}
<span> {span-content} </span>
... </p>

Table

<table><tr><td> {cell_0_0} </td><td> {cell_0_1} </td>
...
</tr><trcolspan=2><td> {merged_cell_1_0} </td>
...
</tr>
...
</table>

Image

<img src="temp_depdf/$prefix.png"></img>

Configuration encyclopedia

PDF 解析

keyworddetaildefault
logo_flag是否分析不同页面共有的水印信息True
header_footer_flag是否分析不同页面共有的页眉页脚信息True
temp_dir_prefix是否分析不同页面共有的页眉页脚信息temp_depdf
unique_prefix生成临时文件图片的文件名称(一般会自动生成)

页面解析

keyworddetaildefault
table_flag是否解析表格True
paragraph_flag是否解析段落True
image_flag是否解析图片True
resolutiondebug 模式下生成页面预览图的分辨率300
main_frame_tolerance识别页面内主要文字区域的阈值
x_tolerance识别页面内文本行的横向阈值
y_tolerance识别页面内文本行的纵向阈值
page_num_top_fraction识别页面内页码信息上边界距离和页面的高度比例
page_num_left_fraction识别页面内页码信息
page_num_right_fraction识别页面内页码信息

页面分栏识别

keyworddetaildefault
multiple_columns_flag是否识别多栏页面True
max_columns识别多栏页面栏数上限3
column_region_half_width识别多栏页面栏分界宽度
min_column_region_objects识别多栏页面栏分界内的对象数目上限

字符提取

keyworddetaildefault
char_overlap_size判断字符是否重叠的阈值
default_char_size默认的字符大小
char_size_upper探测到字符大小的上限
char_size_lower探测到字符大小的下限

表格提取

keyworddetaildefault
dotted_line_flag是否分析页面内的虚线
curved_line_flag是否分析页面内的曲线
snap_flag是否合并表格线段
add_line_flag是否为表格增加横竖线
min_double_line_tolerance判断线段是否为临近双线的距离下限
max_double_line_tolerance判断线段是否为临近双线的距离上限
vertical_double_line_tolerance判断线段是否为垂直临近双线的距离上限
table_cell_merge_tolerance合并单元格的宽度差别容错值
skip_empty_table是否忽略空白表格
add_vertical_lines_flag是否增加竖线
add_horizontal_lines_flag是否增加横线
add_horizontal_line_tolerance增加横线的阈值

图片提取

keyworddetaildefault
min_image_size识别图片的边长最小像素值80
image_resolution提取图片的分辨率300

页眉页脚识别

keyworddetaildefault
default_head_tail_page_offset_percent页眉页脚的错位比例

日志输出

keyworddetaildefault
log_level日志的级别WARNING
verbose_flag是否输出运行中间过程信息False
debug_flag是否打开调试(生成解析对象的边界信息)False

生成的网页标签

keyworddetaildefault
span_class生成 HTML 的 span 节点的 classpdf-span
paragraph_class生成 HTML 的 p 节点的 classpdf-paragraph
table_class生成 HTML 的 table 节点的 classpdf-table
pdf_class生成 HTML 的最外层 pdf div 节点的 classpdf-content
image_class生成 HTML 的 img 节点的 classpdf-image
page_class生成 HTML 的 page div 的 classpdf-page
mini_page_class生成 HTML 的 mini-page div 的 classpdf-mini-page

Update log

  • 2020-03-18 add support for multiple-column PDFs
  • 2020-03-12 initial depdf release

Appendix

todo

  • add support for multiple-column pdf page
  • better table structure recognition
  • recognize embedded objects inside page elements

Project overview

.
├── depdf
│ ├── components
│ │ ├── image.py
│ │ ├── paragraph.py
│ │ ├── span.py
│ │ ├── table.py
│ │ └── text.py
│ ├── api.py
│ ├── base.py
│ ├── config.py
│ ├── error.py
│ ├── log.py
│ ├── page.py
│ ├── page_tools.py
│ ├── pdf.py
│ ├── pdf_tools.py
│ ├── settings.py
│ ├── utils.py
│ └── version.py
├── test
│ ├── test.html
│ ├── test.pdf
│ ├── test.py
│ └── test_mc.pdf
├── LICENSE
├── README.md
├── annotations.jpg
└── pyproject.toml
4 directories, 33 files

DePage element denotations

Useful element properties within page

page element

About

An ultimate pdf file disintegration tool

Topics

Resources

Stars

11 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages