From 63751efa9be29a6c5e4e5e0f345db0e2f17fa256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AF=92=E5=BD=B1=E5=B3=B0?= <1499263074@qq.com> Date: Fri, 27 May 2022 17:59:00 +0800 Subject: [PATCH 1/6] =?UTF-8?q?Excel=E8=AF=BB=E5=8F=96=E7=AD=89=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contributors/han_ying_feng/office/excel.py | 113 +++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 contributors/han_ying_feng/office/excel.py diff --git a/contributors/han_ying_feng/office/excel.py b/contributors/han_ying_feng/office/excel.py new file mode 100644 index 0000000..86a1ed1 --- /dev/null +++ b/contributors/han_ying_feng/office/excel.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python +# -*- coding:utf-8 -*- + +############################################# +# File Name: excel.py +# Mail: 1957875073@qq.com +# Created Time: 2022-4-25 10:17:34 +# Description: 有关 excel 的自动化操作 +############################################# +import math + +import numpy +from faker import Faker +import pandas as pd +from alive_progress import alive_bar + +from utils import pandas_mem + + +def fake2excel(columns=['name'], rows=1, language='zh_CN', path='./fake2excel.xlsx', ): + """ + @Author & Date : CoderWanFeng 2022/5/13 0:12 + @Desc : columns:list,每列的数据名称,默认是名称 + rows:多少行,默认是1 + language:什么语言,可以填english,默认是中文 + path:输出excel的位置,有默认值 + """ + # 可以选择英语 + if language.lower() == 'english': + language = 'en_US' + # 开始造数 + fake = Faker(language) + excel_dict = {} + with alive_bar(len(columns) * rows) as bar: + for column in columns: + excel_dict[column] = list() + # excel_dict[column] = map(lambda x: eval('fake.{func}()'.format(func=x)), [column] * rows) # 使用map,会报错 + while len(excel_dict[column]) < rows: + excel_dict[column].append(eval('fake.{func}()'.format(func=column))) + bar() + # 用pandas,将模拟数据,写进excel里面 + data = pd.DataFrame(excel_dict) + data = pandas_mem.reduce_pandas_mem_usage(data) + __excelWriter__(path=path, data=data) + + +def __excelWriter__(path, data: pd.DataFrame): + # 使用文件路径创建ExcelWriter对象 + writer = pd.ExcelWriter(path) + # 将数据写入到writer对象中 + data.to_excel(writer, index=False) + # 保存文件 + writer.save() + # 关闭资源 + writer.close() + + +############################################# +# Author: han_ying_feng +# Mail: gyuanhao@163.com +# Created Time: 2022-05-26 12:13:09 +# Description: 根据表名读取Excel数据 +############################################# +def read_all_excel(excel_file_path, sheet_name='Sheet1', header=0) -> pd.DataFrame: + return read_cols_to_name(excel_file_path, sheet_name=sheet_name, header=header) + + +############################################# +# Author: han_ying_feng +# Mail: gyuanhao@163.com +# Created Time: 2022-05-26 12:13:09 +# Description: 读取Excel某列数据 +############################################# +def read_cols_to_name(excel_file_path, cols_name=None, header=0, sheet_name='Sheet1') -> pd.DataFrame: + data = pd.read_excel(excel_file_path, usecols=cols_name, header=header, sheet_name=sheet_name) + return data + + +############################################# +# Author: han_ying_feng +# Mail: gyuanhao@163.com +# Created Time: 2022-05-26 12:13:03 +# Description: 计算某列数据的和,并返回 +############################################# +def sum_by_cols(excel_file_path, cols_name=[], header=0, sheet_name='Sheet1'): + # 读取数据表 + cols_data_list = read_cols_to_name(excel_file_path, cols_name=cols_name, header=header, sheet_name=sheet_name) + # 获取数据表中的数据 + values = cols_data_list.values + # 将获取到的数据更改为list + num_list = [] + for item in values: + # 判断整列中的数据是否是数字类型 + if type(item[0]) == int or type(item[0]) == numpy.int64: + num_list.append(item[0]) + return math.fsum(num_list) + + +############################################# +# Author: han_ying_feng +# Mail: gyuanhao@163.com +# Created Time: 2022-05-26 12:13:03 +# Description: 将一段数据添加到指定列的最后一个单元格 +############################################# +def append_by_cols(data, excel_file_path, cols_name=[], sheet_name='Sheet1', header=0, axis=0): + # 读取整个表的内容 + read_data = read_all_excel(excel_file_path, sheet_name, header=header) + # 创建新数据的Frame + df = pd.DataFrame(data=[data], columns=cols_name) + # 新数据与读取到的整个表的数据合并 + data = pd.concat([read_data, df], ignore_index=True, axis=axis) + # 写入原文件中 + __excelWriter__(excel_file_path, data) -- Gitee From 29039f8921d68ff982a9881b9d8408d3be793a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AF=92=E5=BD=B1=E5=B3=B0?= <1499263074@qq.com> Date: Sat, 28 May 2022 11:19:39 +0800 Subject: [PATCH 2/6] =?UTF-8?q?Excel=E8=AF=BB=E5=8F=96=E7=AD=89=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contributors/han_ying_feng/office/excel.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contributors/han_ying_feng/office/excel.py b/contributors/han_ying_feng/office/excel.py index 86a1ed1..af8a163 100644 --- a/contributors/han_ying_feng/office/excel.py +++ b/contributors/han_ying_feng/office/excel.py @@ -72,8 +72,7 @@ def read_all_excel(excel_file_path, sheet_name='Sheet1', header=0) -> pd.DataFra # Description: 读取Excel某列数据 ############################################# def read_cols_to_name(excel_file_path, cols_name=None, header=0, sheet_name='Sheet1') -> pd.DataFrame: - data = pd.read_excel(excel_file_path, usecols=cols_name, header=header, sheet_name=sheet_name) - return data + return pd.read_excel(excel_file_path, usecols=cols_name, header=header, sheet_name=sheet_name) ############################################# -- Gitee From 7b96df5553f75d0989434a45b4a4a3d9bb87ab00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AF=92=E5=BD=B1=E5=B3=B0?= <1499263074@qq.com> Date: Sun, 29 May 2022 18:27:42 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=AE=8C=E5=96=84excel.py=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contributors/han_ying_feng/office/excel.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/contributors/han_ying_feng/office/excel.py b/contributors/han_ying_feng/office/excel.py index af8a163..68cf2d9 100644 --- a/contributors/han_ying_feng/office/excel.py +++ b/contributors/han_ying_feng/office/excel.py @@ -41,9 +41,15 @@ def fake2excel(columns=['name'], rows=1, language='zh_CN', path='./fake2excel.xl # 用pandas,将模拟数据,写进excel里面 data = pd.DataFrame(excel_dict) data = pandas_mem.reduce_pandas_mem_usage(data) - __excelWriter__(path=path, data=data) + __excelWriter__(path=path, data=data) +############################################# +# Author: han_ying_feng +# Mail: gyuanhao@163.com +# Created Time: 2022-05-26 12:13:09 +# Description: 将想要添加的DataFrame 写入到相应Path的Excel文件中 +############################################# def __excelWriter__(path, data: pd.DataFrame): # 使用文件路径创建ExcelWriter对象 writer = pd.ExcelWriter(path) @@ -99,7 +105,7 @@ def sum_by_cols(excel_file_path, cols_name=[], header=0, sheet_name='Sheet1'): # Author: han_ying_feng # Mail: gyuanhao@163.com # Created Time: 2022-05-26 12:13:03 -# Description: 将一段数据添加到指定列的最后一个单元格 +# Description: 将一段数据添加到指定列中的单元格内 ############################################# def append_by_cols(data, excel_file_path, cols_name=[], sheet_name='Sheet1', header=0, axis=0): # 读取整个表的内容 -- Gitee From 2fefeb2c1f0e6b31b8acafcc3cf9c5c2a915edca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AF=92=E5=BD=B1=E5=B3=B0?= <1499263074@qq.com> Date: Sun, 29 May 2022 19:50:22 +0800 Subject: [PATCH 4/6] =?UTF-8?q?invalid=20syntax=20=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contributors/han_ying_feng/office/excel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contributors/han_ying_feng/office/excel.py b/contributors/han_ying_feng/office/excel.py index 68cf2d9..3dc95c8 100644 --- a/contributors/han_ying_feng/office/excel.py +++ b/contributors/han_ying_feng/office/excel.py @@ -50,7 +50,7 @@ def fake2excel(columns=['name'], rows=1, language='zh_CN', path='./fake2excel.xl # Created Time: 2022-05-26 12:13:09 # Description: 将想要添加的DataFrame 写入到相应Path的Excel文件中 ############################################# -def __excelWriter__(path, data: pd.DataFrame): +def __excelWriter(path, data: pd.DataFrame): # 使用文件路径创建ExcelWriter对象 writer = pd.ExcelWriter(path) # 将数据写入到writer对象中 -- Gitee From 7ae8c1ce8f5a35f7cc7a076a3d1cf5e199203652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AF=92=E5=BD=B1=E5=B3=B0?= <1499263074@qq.com> Date: Sun, 29 May 2022 19:53:59 +0800 Subject: [PATCH 5/6] =?UTF-8?q?ExcelWriter=E6=96=B9=E6=B3=95=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contributors/han_ying_feng/office/excel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contributors/han_ying_feng/office/excel.py b/contributors/han_ying_feng/office/excel.py index 3dc95c8..68cf2d9 100644 --- a/contributors/han_ying_feng/office/excel.py +++ b/contributors/han_ying_feng/office/excel.py @@ -50,7 +50,7 @@ def fake2excel(columns=['name'], rows=1, language='zh_CN', path='./fake2excel.xl # Created Time: 2022-05-26 12:13:09 # Description: 将想要添加的DataFrame 写入到相应Path的Excel文件中 ############################################# -def __excelWriter(path, data: pd.DataFrame): +def __excelWriter__(path, data: pd.DataFrame): # 使用文件路径创建ExcelWriter对象 writer = pd.ExcelWriter(path) # 将数据写入到writer对象中 -- Gitee From e6d11bd3c17aed6ae85f0590f689bd2329dceb30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AF=92=E5=BD=B1=E5=B3=B0?= <1499263074@qq.com> Date: Sun, 29 May 2022 19:54:12 +0800 Subject: [PATCH 6/6] =?UTF-8?q?ExcelWriter=E6=96=B9=E6=B3=95=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contributors/han_ying_feng/office/excel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contributors/han_ying_feng/office/excel.py b/contributors/han_ying_feng/office/excel.py index 68cf2d9..3dc95c8 100644 --- a/contributors/han_ying_feng/office/excel.py +++ b/contributors/han_ying_feng/office/excel.py @@ -50,7 +50,7 @@ def fake2excel(columns=['name'], rows=1, language='zh_CN', path='./fake2excel.xl # Created Time: 2022-05-26 12:13:09 # Description: 将想要添加的DataFrame 写入到相应Path的Excel文件中 ############################################# -def __excelWriter__(path, data: pd.DataFrame): +def __excelWriter(path, data: pd.DataFrame): # 使用文件路径创建ExcelWriter对象 writer = pd.ExcelWriter(path) # 将数据写入到writer对象中 -- Gitee