吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 8028|回复: 95
收起左侧

[Python 转载] 【Python】一键提取pdf文件表格数据,真香,还有可视化界面

    [复制链接]
mrliu133 发表于 2021-11-21 21:08
废话不多说,直接上代码
[Python] 纯文本查看 复制代码
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
[url=home.php?mod=space&uid=267492]@file[/url]    :   pdftablekiller.py
[url=home.php?mod=space&uid=238618]@Time[/url]    :   2021/11/21 
[url=home.php?mod=space&uid=686208]@AuThor[/url]  :   Ljujl 
[url=home.php?mod=space&uid=1248337]@version[/url] :   1.0
@Contact :   [email]mr_liu133299@foxmail.com[/email]
'''

# here put the import lib
from tkinter import Label, constants, filedialog, Button, Frame, BROWSE, BOTH, Text, INSERT, EXTENDED
from tkinter import Entry, StringVar, messagebox, Tk, mainloop
from tkinter.ttk import Treeview
import camelot
"""
conda 安装 camelot
"""


class MainWindow():
    def __init__(self) -> None:
        self.root = Tk()
        self.root.title("Pdf Table Killer")
        self.root.iconbitmap(r"pdf表格提取\test1.ico")
        x = (self.root.winfo_screenwidth() - self.root.winfo_reqwidth()) // 4
        y = (self.root.winfo_screenheight() - self.root.winfo_reqheight()) // 4

        frame = Frame(self.root)
        frame.pack(padx=2, pady=2, ipadx=1)

        bg = "#DCDCDC"
        btn_open = Button(frame, text="open", width=5, height=1, command=self.open_pdf,).grid(
            row=0, column=0, padx=5, pady=5, rowspan=2, columnspan=2, sticky='NS')  #

        label_page = Label(frame, text="page number:", width=20, height=1, bg=bg).grid(
            row=0, column=2, padx=5, pady=5, sticky='W')
        self.pages = StringVar()
        page_input = Entry(frame, bd=1, textvariable=self.pages).grid(
            row=0, column=3, padx=5, pady=5, sticky='W')

        label_row_tl = Label(frame, text="row_tol(optional int):", width=20, height=1, bg=bg).grid(
            row=1, column=2, padx=5, pady=5, sticky='W')
        self.row_tol_value = StringVar()
        row_tol = Entry(frame, bd=1, textvariable=self.row_tol_value).grid(
            row=1, column=3, padx=5, pady=5, sticky='W')

        btn_process = Button(frame, text="process", width=10, height=1, command=self.process).grid(
            row=0, column=6, padx=5, pady=5, sticky='W')
        btn_save = Button(frame, text="save", width=10, height=1, command=self.sava_data).grid(
            row=1, column=6, padx=5, pady=5, sticky='W')

        label_display_table = Label(frame, text="display table:", height=1, bg=bg).grid(
            row=2, columnspan=7, sticky='WE')
        label_white = Label(frame, text="", height=1, bg=bg).grid(
            row=3, columnspan=7, sticky='WE')
        readme = """Notes:
1. click "open" button
2. input page number
3. click "process" button(maybe you need wait a few seconds)
4. click "save" button
5. any questions please make contact with me: [email]mr_liu133299@foxmail.com[/email]
        """
        readme_text = Text(frame, height=8)
        readme_text.insert(INSERT, readme)
        readme_text['state'] = "disabled"
        readme_text.grid(
            row=0, column=8, rowspan=6, sticky='SN')  # , padx=5, pady=5, column=8

    def open_pdf(self):
        self.filepath = filedialog.askopenfilename(title='Please choose a file', filetypes=[
            ('Pdf file', '*.pdf')])

    def process(self):
        # page为阅读显示的页码,不同于文献页脚页码
        if self.filepath and self.pages.get():
            if self.row_tol_value.get():
                self.root.geometry(
                    f"{self.root.winfo_screenwidth()}x{self.root.winfo_screenheight()}+0+0")
                tables = camelot.read_pdf(self.filepath, pages=self.pages.get(
                ), flavor='stream', row_tol=eval(self.row_tol_value.get()))
                self.df = tables[0].df
                data = self.df.to_dict(orient="records")
                # 定义列的名称
                columns = tuple(self.df.columns)
                tree = Treeview(self.root, show="headings",
                                selectmode=BROWSE, columns=columns)
                # 设置表格文字居中
                for col in self.df.columns:
                    tree.column(col, anchor="center")

                # 设置表格头部标题
                for c in columns:
                    tree.heading(c, text=c)

                # 设置表格内容
                i = 0
                for v in data:
                    tree.insert('', i, values=tuple(v.values()))
                    i += 1
                tree.pack(expand=True, fill=BOTH)
            else:
                self.root.geometry(
                    f"{self.root.winfo_screenwidth()}x{self.root.winfo_screenheight()}+0+0")
                tables = camelot.read_pdf(
                    self.filepath, pages=self.pages.get(), flavor='stream')
                self.df = tables[0].df
                data = self.df.to_dict(orient="records")
                # 定义列的名称
                columns = tuple(self.df.columns)
                tree = Treeview(self.root, show="headings",
                                selectmode=EXTENDED, columns=columns)
                # 设置表格文字居中
                for col in self.df.columns:
                    tree.column(col, anchor="center")

                # 设置表格头部标题
                for c in columns:
                    tree.heading(c, text=c)

                # 设置表格内容
                i = 0
                for v in data:
                    tree.insert('', i, values=tuple(v.values()))
                    i += 1
                tree.pack(expand=True, fill=BOTH)

    # 保存文件
    def sava_data(self):
        file_name = self.filepath.split(
            "/")[-1].split(".")[0] + "_page" + self.pages.get() + ".csv"
        self.df.to_csv(f"{file_name}", index=False, header=None)
        messagebox.showinfo('tips', 'save successfully~~')


if __name__ == "__main__":
    main = MainWindow()
    mainloop()
pic4.jpg
pic3.jpg
pic2.jpg
pic1.jpg

免费评分

参与人数 29吾爱币 +32 热心值 +26 收起 理由
kongdang1 + 1 我很赞同!
mhsz + 1 + 1 谢谢@Thanks!
uilyuan + 1 + 1 谢谢@Thanks!
Undefine + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
Jason-M + 1 + 1 热心回复!
tutu75738 + 1 谢谢@Thanks!
随遇而安8 + 1 + 1 我是来研究代码的
rock1102 + 1 + 1 我很赞同!
搬砖孩子要毕业 + 1 + 1 谢谢@Thanks!
xhf34242 + 1 + 1 我很赞同!
mofeng2098 + 1 谢谢@Thanks!
99248688 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
jlzoe + 1 + 1 谢谢@Thanks!
钓不到的鱼竿 + 1 + 1 谢谢@Thanks!
王星星 + 1 + 1 谢谢@Thanks!
Hungchuiho + 1 谢谢@Thanks!
aa530416 + 1 + 1 热心回复!
metaxman + 1 谢谢@Thanks!
ballack_13 + 1 + 1 谢谢@Thanks!
1570707894 + 1 + 1 鼓励转贴优秀软件安全工具和文档!
神马还在浮云里 + 1 <font style="vertical-align: inherit;"><font style=
lizy169 + 1 + 1 谢谢@Thanks!
chriskw + 1 + 1 我很赞同!
superlaomao + 2 + 1 谢谢@Thanks!
Anjou521 + 1 + 1 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
yanglinman + 1 + 1 期待成品!
独念成慕 + 1 + 1 谢谢@Thanks!
火焰加鲁鲁 + 1 + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

咋说吧 发表于 2021-11-26 15:31
看文献的时候会用的,不错!
blackboard 发表于 2021-11-21 21:52
 楼主| mrliu133 发表于 2021-11-21 21:23
火焰加鲁鲁 发表于 2021-11-21 21:13
感谢楼主,楼主有成品么

今天尝试了打包,打包体积800多M,而且还存在问题,就没弄了。主要是camlot这个模块依赖的包有点多,另外pandas也很大。
成品目前就是代码,运行可以出结果。图标路径可以修改一下或者注释掉。
头像被屏蔽
onetwothreenb 发表于 2021-11-30 13:40
提示: 作者被禁止或删除 内容自动屏蔽
火焰加鲁鲁 发表于 2021-11-21 21:13
感谢楼主,楼主有成品么
suqingxiao 发表于 2021-11-21 22:00
这个是只能用于那种表格转出来的PDF吗?
aysta 发表于 2021-11-21 22:06
感谢分享,不错
zc777 发表于 2021-11-21 22:20
牛呀,我学习学习了
小能维尼 发表于 2021-11-21 22:24
如果不打包,有没有体积一说??
库我都装,直接跑代码的那种
库是不是也挺大的?
panchun888 发表于 2021-11-21 22:28
学习了,谢谢楼主
火焰加鲁鲁 发表于 2021-11-21 22:58
mrliu133 发表于 2021-11-21 21:23
今天尝试了打包,打包体积800多M,而且还存在问题,就没弄了。主要是camlot这个模块依赖的包有点多,另外 ...

好的,感谢
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-26 14:38

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表