当前位置:首页>python>《别再手动录入磅单了!Python调用百度OCR大模型API批量识别磅单并导出Excel,效率飙升10倍!》

《别再手动录入磅单了!Python调用百度OCR大模型API批量识别磅单并导出Excel,效率飙升10倍!》

  • 2026-09-04 11:51:41
《别再手动录入磅单了!Python调用百度OCR大模型API批量识别磅单并导出Excel,效率飙升10倍!》

“带你横跨办公自动化的数据江海”

@摸鱼

闻道有先后,术业有专攻。

各位大佬好!

~我依旧是你们的老朋友摸鱼~

♥ 1

在职场摸爬滚打的这十多年里,我用Python悄悄干了不少“正事”——不知不觉攒下了一整套办公自动化的实用项目技巧。去年10月初创立了公众号 「码海听潮」 ,初衷很简单:把重复的劳动交给代码,把摸鱼的时间留给生活。

目前已经吭哧吭哧更新了近90篇原创文章,每一篇都是实操干货,不讲虚的,只聊怎么用代码真正解放双手,帮大家早点下班、准点摸鱼

♥ 2

♥ 3好叻,多了不说,少了不唠,咱直接上干货。

办公需求场景

从崩溃到优雅的进化

有一个神秘的图片文件夹,里面有多张磅单图片,现在的需求是批量识别磅单上的'车牌号'、 '打印时间'、'毛重'、 '皮重'、 '净重'、 '发货单位'、 '收货单位'、 '送货单号'等信息并保存为excel文档,要是这种类似的需求你的Big Boss安排你去完成,请问阁下该如何应对?

  • 需求的磅单图片文件列表如下图:

  • 需求的磅单图片

办公痛点分析

01

痛点一:效率极其低下,耗时耗力

    •  人工需要一张张打开图片,找到对应的字段,然后一个字一个字敲进Excel。如果每天有几十上百张磅单,这项工作会占据大量工作时间。

    02

    痛点二:容易视觉疲劳,导致录入错误

      • 长时间盯着屏幕看不同清晰度、不同字体甚至手写的磅单,很容易看错导致录入时候串行错位。

      由此可见若下载成百上千个图片的话整个操作流程繁琐且耗时,高频次的鼠标点击和键盘输入使操作者手指疲劳,堪称"键盘敲冒烟"式的体力劳动,加上人工疲劳操作极易导致遗漏文件夹。于是乎这时候,按以往的 “解题套路”,Python 的专属 BGM 该响起来了 ——go~ go~ go~,救苦救难的大救星这不就来了!!

      @摸鱼

      问题拆解思路

      1.遍历磅单图片文件夹→

      2.调用百度OCR大模型API对其图片进行指定的信息提取保存为excel。

      下面,我就用python代码让excel见识一下,什么叫"传统文化遇上赛博效率"(仅展示部分代码,非完整代码,需完整代码看文章末尾说明~)

      import sysfrom PyQt6.QtWidgets import (    QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,    QPushButton, QLabel, QLineEdit, QFileDialog,    QProgressBar, QTableWidget, QTableWidgetItem, QHeaderView,    QGroupBox, QSplitter, QStatusBar)from PyQt6.QtCore import Qtfrom PyQt6.QtGui import QFont, QColorclass MainWindow(QMainWindow):    def __init__(self):        super().__init__()        self.init_ui()    def init_ui(self):        """初始化UI"""        self.setWindowTitle("调用baiduocr大模型api批量识别磅单(欢迎关注微信公众号:码海听潮)")        self.setMinimumSize(800, 700)        # 设置全局样式        self.setStyleSheet("""            QMainWindow {                background-color: #f5f6fa;            }            QGroupBox {                font-weight: bold;                border: 2px solid #dcdde1;                border-radius: 8px;                margin-top: 10px;                padding-top: 10px;                background-color: white;            }            QGroupBox::title {                subcontrol-origin: margin;                left: 10px;                padding: 0 5px 0 5px;                color: #273c75;            }            QPushButton {                background-color: #273c75;                color: white;                border: none;                padding: 10px 20px;                border-radius: 5px;                font-weight: bold;                min-width: 100px;            }            QPushButton:hover {                background-color: #192a56;            }            QPushButton:disabled {                background-color: #7f8c8d;            }            QPushButton#danger {                background-color: #c23616;            }            QPushButton#danger:hover {                background-color: #962d22;            }            QPushButton#success {                background-color: #44bd32;            }            QPushButton#success:hover {                background-color: #2e7d32;            }            QLineEdit {                padding: 8px;                border: 1px solid #dcdde1;                border-radius: 5px;                background-color: white;            }            QLineEdit:focus {                border: 2px solid #273c75;            }            QTableWidget {                border: 1px solid #dcdde1;                border-radius: 5px;                background-color: white;                gridline-color: #dcdde1;            }            QTableWidget::item {                padding: 5px;            }            QTableWidget::item:selected {                background-color: #273c75;                color: white;            }            QHeaderView::section {                background-color: #273c75;                color: white;                padding: 8px;                border: none;                font-weight: bold;            }            QProgressBar {                border: 1px solid #dcdde1;                border-radius: 5px;                text-align: center;                background-color: white;                height: 20px;            }            QProgressBar::chunk {                background-color: #44bd32;                border-radius: 5px;            }            QStatusBar {                background-color: #273c75;                color: white;            }        """)        # 创建中央部件        central_widget = QWidget()        self.setCentralWidget(central_widget)        # 主布局        main_layout = QVBoxLayout(central_widget)        main_layout.setSpacing(10)        main_layout.setContentsMargins(15, 15, 15, 15)        # 创建分割器        splitter = QSplitter(Qt.Orientation.Vertical)        main_layout.addWidget(splitter)        # 上部控制区域        top_widget = QWidget()        top_layout = QVBoxLayout(top_widget)        splitter.addWidget(top_widget)        # 文件选择区域        file_group = QGroupBox("文件选择")        file_layout = QHBoxLayout(file_group)        self.path_edit = QLineEdit()        self.path_edit.setPlaceholderText("请选择图片目录...")        self.browse_btn = QPushButton("浏览目录")        file_layout.addWidget(self.path_edit)        file_layout.addWidget(self.browse_btn)        top_layout.addWidget(file_group)        # API设置区域        api_group = QGroupBox("API设置")        api_layout = QHBoxLayout(api_group)        api_layout.addWidget(QLabel("API Key:"))        self.api_key_edit = QLineEdit()        self.api_key_edit.setPlaceholderText("请输入API Key")        api_layout.addWidget(self.api_key_edit)        api_layout.addWidget(QLabel("Secret Key:"))        self.secret_key_edit = QLineEdit()        self.secret_key_edit.setPlaceholderText("请输入Secret Key")        api_layout.addWidget(self.secret_key_edit)        self.save_api_btn = QPushButton("保存设置")        api_layout.addWidget(self.save_api_btn)        top_layout.addWidget(api_group)        # 进度区域        progress_group = QGroupBox("处理进度")        progress_layout = QVBoxLayout(progress_group)        self.progress_bar = QProgressBar()        progress_layout.addWidget(self.progress_bar)        self.status_label = QLabel("就绪")        self.status_label.setStyleSheet("color: #7f8c8d;")        progress_layout.addWidget(self.status_label)        top_layout.addWidget(progress_group)        # 按钮区域(居中显示)        button_layout = QHBoxLayout()        button_layout.setAlignment(Qt.AlignmentFlag.AlignCenter)        self.start_btn = QPushButton("开始识别")        self.start_btn.setObjectName("success")        self.start_btn.setEnabled(False)        self.stop_btn = QPushButton("取消识别")        self.stop_btn.setObjectName("danger")        self.stop_btn.setEnabled(False)        self.export_btn = QPushButton("导出Excel")        button_layout.addWidget(self.start_btn)        button_layout.addWidget(self.stop_btn)        button_layout.addWidget(self.export_btn)        top_layout.addLayout(button_layout)        # 下部结果表格        bottom_widget = QWidget()        bottom_layout = QVBoxLayout(bottom_widget)        splitter.addWidget(bottom_widget)        result_group = QGroupBox("识别结果")        result_layout = QVBoxLayout(result_group)        # 创建表格        self.table = QTableWidget()        self.table.setColumnCount(9)        self.table.setHorizontalHeaderLabels([            '文件名', '车牌号', '打印时间', '毛重', '皮重',             '净重', '发货单位', '收货单位', '送货单号'        ])        # 设置表格列宽平均分布        header = self.table.horizontalHeader()        for i in range(9):            header.setSectionResizeMode(i, QHeaderView.ResizeMode.Stretch)        result_layout.addWidget(self.table)        bottom_layout.addWidget(result_group)        # 设置分割器比例        splitter.setSizes([350, 350])        # 状态栏        self.status_bar = QStatusBar()        self.setStatusBar(self.status_bar)        self.status_bar.showMessage("就绪")def main():    app = QApplication(sys.argv)    # 设置应用程序字体    font = QFont("微软雅黑", 9)    app.setFont(font)    # 创建并显示主窗口    window = MainWindow()    window.show()    sys.exit(app.exec())if __name__ == "__main__":    main()

      最终将所有磅单图片的信息提取保存到excel表格,实现了之前既定的需求....

      通过上面Python自动化脚本,仅用几分钟的时间就完成原需手动操作数小时甚至数天的工作任务。从最初准备手动人工机械操作的麻木到用python实现高效自动化的畅快,工作效率获得指数级提升,终于实现了不加班熬夜的自由!

      大佬们也可以举一反三,参照上面的代码思路根据自己工作中的实际情况来具体问题具体分析,实现自己定制化的需求。

      结语

      当Python遇见办公,牛马打工人终于笑出了猪叫声

      【职场人必看】每天早上一睁眼,想到又要面对:

      1.📊 堆积如山的Excel表格

      2.📑 机械重复的复制粘贴

      3.✍️ 永远改不完的各类文档

      4.诸如此类的更多........

      是不是连Ctrl+Alt+Delete的心都有了?

      别慌!别急,摸鱼这位“职场外挂”已经带着Python代码来拯救你了!

      友情提示:考虑到没有python环境的朋友需要打包好的成品exe,摸鱼早已贴心打包好,本篇文章代码打包的exe截图如下:

      另外,《码海听潮》公众号所有文章码和exe程序已打包好上传绿联nas私有云,有需要的大佬扫一扫上面博主的个人微信二维码,需要的大佬需支付9.9元永久拥有公众号资源(写原创干货费时费力,属实不易),邀请您进入社区群获取下载链接!!,群内提供python办公自动化交流问题,解决问题,且码海听潮微信公众号文章发布会第一时间会更新到群里,非诚勿扰哈!

      码海听潮官方社区群如下:

      赶紧微信扫一扫下方二维码添加摸鱼君微信

      最新文章

      随机文章