zoukankan      html  css  js  c++  java
  • python数据处理excel和pdf,并打包成exe

    之前零散的用过一点python做数据处理,这次又遇到一个数据处理的小功能,因此,记录一下整个流程,方便以后查阅。

    功能要求:读取excel,找指定的PDF文件的页数是否与excel中记录的一致

    整个处理过程包括python环境配置,插件安装,excel和PDF处理,exe打包

    1、python环境配置

    IDE用的是PyCharm社区版,pyhon环境用的是pandas,它内嵌了很多数据处理的插件,就有我们这次需要的excel处理插件。

    安装其他插件,PDF处理采用PyPDF2,exe打包采用pyinstaller

    2、excel和PDF处理

    整个代码就不贴了,太多了也不想看,下面说一下主要代码块

    1)excel读写

    import pandas as pd
    
    # 读取excel文件,configPath为excel文件路径,configSheetName为excel中sheet表单名称
    configTable = pd.read_excel(configPath, configSheetName)
    # 读取表单中的数据,返回一个数组,数组存储每行的信息,fieldCount为表单数据列总数
    configUnit = configTable.iloc[:, range(fieldCount)]
    
    configCount = len(configUnit)
    for k in range(configCount):
      # 读取第k行的具体列数据,loc的第二个参数是列名称
      pdfNm = NameUnit.loc[k, pdfNmName]
      fileNum = NameUnit.loc[k, fileNmName]
       
        # 省略若干代码...
    
    # 创建excel对象
    writer = pd.ExcelWriter(excelPath + u'_结果.xlsx', engine='xlsxwriter')
    NameUnit.to_excel(writer, sheet_name=configSheetName)
    workbook
    = writer.book worksheet = writer.sheets[configSheetName] for k in range(configCount): # 省略若干代码... if pageCount != pageNum: # 如果excel中记录值与实际值不相等 format1 = workbook.add_format({'bg_color': 'red'}) # 红色 else: # 相等 format1 = workbook.add_format({'bg_color': 'transparent'}) # 白色 # 设置excel单元格格式 worksheet.conditional_format(color_range, {'type': 'no_blanks', 'format': format1}) worksheet.conditional_format(color_range, {'type': 'blanks', 'format': format1})
    # 保存excel writer.save()

    2)PDF读取

    import PyPDF2
    import os
    
    # 判断文件是否存在
    if os.path.exists(pdfFilePath):
        # 获取PDF对象
        pageObj = PyPDF2.PdfFileReader(pdfFilePath)
        # 获取PDF页码总数
        pageNum = pageObj.getNumPages()

    3、exe打包

    1)配置PyInstaller

    参数-F表示打包成一个exe文件,不带-F则打包成一个文件夹,里面很多小文件,前一个运行速度慢一点,后一种运行速度快一点

    2、遇到的问题

    错误1:

    RecursionError: maximum recursion depth exceeded

    解决:在对应的spec文件前面添加最大的行数限制

    import sys
    sys.setrecursionlimit(5000)

    错误2:

    No module named 'pandas._libs.tslibs.timedeltas' in PyInstaller 

    在pandas安装路径下,Anaconda3Libsite-packagesPyInstallerhooks新建hook-pandas.py文件,并根据报错信息添加缺少的模块,以下是我添加的所有依赖模块

    hiddenimports=[
        #all your previous hidden imports
        'pandas', 'pandas._libs.tslibs.np_datetime', 'pandas._libs.tslibs.nattype',
        'pandas._libs.skiplist'
    ]

    点击Tools -> External Tools -> pyinstaller.exe运行spec文件,等个几分钟后就能在工程下的dist文件夹下找到打包的EXE,足足有300M。

      
  • 相关阅读:
    Hihocoder 1275 扫地机器人 计算几何
    CodeForces 771C Bear and Tree Jumps 树形DP
    CodeForces 778D Parquet Re-laying 构造
    CodeForces 785E Anton and Permutation 分块
    CodeForces 785D Anton and School
    CodeForces 785C Anton and Fairy Tale 二分
    Hexo Next 接入 google AdSense 广告
    如何统计 Hexo 网站的访问地区和IP
    Design and Implementation of Global Path Planning System for Unmanned Surface Vehicle among Multiple Task Points
    通过ODBC接口访问人大金仓数据库
  • 原文地址:https://www.cnblogs.com/zhang90030/p/9467585.html
Copyright © 2011-2022 走看看