python: read excel and export excel

news/2024/7/21 6:46:36 标签: python, excel, 开发语言
python">"""
PythonAppReadExcel.py
edit: geovindu,Geovin Du,涂聚文
date 2023-06-13
保险
"""
# This is a sample Python script.
# python.exe -m pip install --upgrade pip
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import xlrd
import xlwt
import xlwings as xw
import xlsxwriter
import openpyxl as ws
import pandas as pd
import pandasql
from pandasql import sqldf
import os
import sys
from pathlib import Path
import re
import Insurance
import ReadExcelData

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm,geovindu,Geovin Du')
    #https://www.digitalocean.com/community/tutorials/pandas-read_excel-reading-excel-file-in-python
    #https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.convert_dtypes.html
    #https://www.geeksforgeeks.org/args-kwargs-python/
    insura=[]
    objlist=[]
    datalist = []
    dulist=[]
    # 查询某文件夹下的文件名
    folderPath = Path(r'C:\\Users\\geovindu\\PycharmProjects\\pythonProject2\\')
    fileList = folderPath.glob('*.xls')
    for i in fileList:
        stname = i.stem
        print(stname)
    # 查询文件夹下的文件  print(os.path.join(path, "User/Desktop", "file.txt"))
    dufile = ReadExcelData.ReadExcelData.ReadFileName(folderPath, 'xls')
    for f in dufile:
        fileurl = os.path.join(folderPath, f)
        dulist1 = ReadExcelData.ReadExcelData.ReadDataFile(fileurl)  # object is not callable 变量名称冲突的原因
        for duobj in dulist1:
            dulist.append(duobj)
        print(os.path.join(folderPath, f))

    ylsum = 0  # 养老
    llsum = 0  # 医疗
    totalsum = 0  # 一年费用
    for geovindu in dulist:
        # duobj = Insurance.Insurance
        print(geovindu)
        name = geovindu.getInsuranceName()
        duname = name.convert_dtypes()
        # yname = duname['Unnamed: 2']
        print(type(duname))
        print("保险类型:", duname)  # class 'pandas.core.series.Series
        strname = pd.Series(duname).values[0]
        coas1 = geovindu.getInsuranceCost()
        # coast = int(geovindu.getInsuranceCost())
        coas = coas1.convert_dtypes()
        coast = pd.Series(coas).values[0]  # int(coas)
        # print("casa",int(coas))
        totalsum = totalsum + coast
        if (strname == "养老"):
            ylsum = ylsum + coast
        if (strname == "医疗"):
            llsum = llsum + coast
        print("费用:", coast)
        month = int(geovindu.getIMonth())
        print("月份:", month)
        datalist.append([strname, coast, month])

    print("一年养老", ylsum)
    print("一年医疗", llsum)
    print("一年费用", totalsum)
    # https: // pandas.pydata.org / pandas - docs / stable / reference / api / pandas.DataFrame.groupby.html
    # 导出数据生成EXCEL
    dataf = pd.DataFrame(datalist, columns=['保险类型', '交费金额', '交费月份'])  # 增加列名称
    dataf2 = pd.DataFrame({"统计类型": ["一年养老", "一年医疗", "一年费用"], "金额": [ylsum, llsum, totalsum]})
    dataf.sort_values('交费月份', inplace=True)  # 指定列排序
    print(sqldf('''SELECT 交费金额,交费月份 FROM dataf group by 交费月份 LIMIT 25'''))
    #staicmont=sqldf('''SELECT 交费金额,交费月份 FROM dataf group by 交费月份 LIMIT 25''')
    # 交费用分份统计
    # print(sqldf('''SELECT 交费金额,交费月份 FROM dataf group by 交费月份  LIMIT 25'''))
    staicmonth = sqldf('''SELECT 交费金额,交费月份 FROM dataf group by 交费月份 LIMIT 25''')

    with pd.ExcelWriter('geovindu.xlsx') as writer:
        dataf.to_excel(writer, sheet_name='2023年保险费用详情', index=False)
        dataf2.to_excel(writer, sheet_name='保险统计', index=False)
        staicmonth.to_excel(writer, sheet_name='月份统计', index=False)


# See PyCharm help at https://www.jetbrains.com/help/pycharm/

 


http://www.niftyadmin.cn/n/433608.html

相关文章

Karl Guttag评Vision Pro:比Quest Pro做了更多正确选择

上周苹果正式发布Vision Pro,尽管要到明年才发售,但光学领域的专业博主Karl Guttag也发表了自己的看法。他提到:目前受邀体验的媒体中要不是苹果粉丝、要不就是对AR、VR了解比较少,没有我看到“批判性思维”或太多对技术分析的内容…

Spark SQL数据源:Hive表

文章目录 一、Spark SQL支持读写Hive二、Spark配置hive-site.xml三、准备工作(一)启动Hive的metastore(二)启动Spark Shell 四、Spark读写Hive数据(一)导入SparkSession(二)创建Spar…

电场探头测量速度的相关定义及解析

大多数电场探头数据表都标注了有关速度参数的规格。但在实际应用中,速度通常有不同的定义,例如测量速度或采样率,这可能造成混淆的情况。本文将介绍与测量速度相关的几种模式及其定义,以及如何正确解释这些定义。 几种定义 采样速…

smart Java——BIO、NIO、AIO的工作流程和代码实现

文章目录 〇、前置知识1.套接字2.线程池 一、BIO1.工作流程2.代码实现3.缺点 二、NIO(基于轮训)1.相比于BIO的优化2.工作流程3.代码实现 三、AIO(基于订阅-通知)1.工作流程2.代码实现 参考 〇、前置知识 1.套接字 在计算机网络编…

jest测试vuex

在vue项目中,vuex是经常使用的一个插件,这个插件用来管理项目中的数据。那么在测试中又应该怎么测试vuex呢?接下来,就说一说测试vuex的方法。测试vuex的方法分为两种,一种是分别测试vuex的组成部分,一种是把…

ChineseBERT使用指北

文章目录 ChineseBert 模型介绍开源代码使用方法未完待续 ChineseBert 模型介绍 论文地址:https://arxiv.org/pdf/2106.16038.pdf 代码地址:https://github.com/ShannonAI/ChineseBert bert是语义模型,因此无法解决形近字、音近字的问题。 …

烟雾调节器

先看效果&#xff1a; 烟雾调节展示 再看代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>可调节的烟雾特效</title><script src"https://cdnjs.cloudflare.com/ajax/…

Linux: 关于blocked for more than 600 seconds的解决方法

背景 在Centos 7的系统日志 /var/log/message中出现大量的 “blocked for more than 600 seconds”和“echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.” 的错误。 如下图所示&#xff1a; 问题原因 默认情况下&#xff0c; Linux最多会使…