本来用excel的宏功能也能实现大部分,但是为了练习python语法,还是用pandas重新弄了一个脚本程序,基本满足需求,后面有时间再部署一个django的平台进行管理。
# -- coding: utf-8 --

import os
import pandas as pd
import configparser as cf

parent_dir = os.path.dirname(os.path.abspath(__file__))
cf = cf.ConfigParser()
try:
    cf.read(parent_dir + "\\config.ini")
except IOError:
    print("Error: 没有找到配置或读取文件失败")
else:
    print("读取配置文件成功")
sections = cf.sections()#获取配置文件中所有sections,sections是列表
# def xlsx(file):
#     file=cf.get('file', 'xlsx')
#
def main():

    workbookname = cf.get('xlsxfile', 'workbookname')
    workbookname=parent_dir+"\\"+workbookname

    sheetname1 = cf.get('xlsxfile', 'sheetname1')
    sheetname2 = cf.get('xlsxfile', 'sheetname2')
    xlsxcolums= cf.get('xlsxfile', 'xlsxcolums')
    sortcolums = cf.get('xlsxfile', 'sortcolums')
    screenkeywords1 = cf.get('xlsxfile', 'screenkeywords1')
    screenfactor1 = cf.get('xlsxfile', 'screenfactor1')
    screenfactor2 = cf.get('xlsxfile', 'screenfactor2')
    screenfactor3 = cf.get('xlsxfile', 'screenfactor3')
    screenfactor4 = cf.get('xlsxfile', 'screenfactor4')
    workbooknamenew = cf.get('xlsxfile', 'workbooknamenew')
    xlsxcolums = xlsxcolums.strip(',').split(',')

    xlsx1 = pd.read_excel(workbookname, sheet_name=sheetname1)
    xlsx2 = pd.read_excel(workbookname, sheet_name=sheetname2)
    xlsx1 = xlsx1.loc[:,xlsxcolums]
    xlsx1 = xlsx1.sort_values(by=sortcolums)
    xlsx1 = xlsx1.loc[(xlsx1[screenkeywords1] == screenfactor1) |(xlsx1[screenkeywords1] == screenfactor2)|(xlsx1[screenkeywords1] == screenfactor3)|(xlsx1[screenkeywords1] == screenfactor4)]
    xlsx2 = xlsx2.loc[:,xlsxcolums]
    xlsx2 = xlsx2.sort_values(by=sortcolums)
    xlsx2 = xlsx2.loc[(xlsx2[screenkeywords1] == screenfactor1) |(xlsx2[screenkeywords1] == screenfactor2)|(xlsx2[screenkeywords1] == screenfactor3)|(xlsx2[screenkeywords1] == screenfactor4)]
    xlsx = xlsx1.append(xlsx2)
    try:
        xlsx.to_excel(parent_dir + "\\" + workbooknamenew)
        print("更新成功")
    except IOError:
        print("Error: 保存文件失败")


    #     xlsx.to_excel(parent_dir+"\\"+workbooknamenew)
    # except IOError:
    #     print("Error: 没有找到配置或读取文件失败")





# def resolveEnv(con):
#     if con.startswith('ENV_'):
#         return os.environ.get(con)
#     return con



if __name__ == "__main__":
    main()

标签: none

添加新评论