From 5a59812484b309ac07814937621f86363c5a3861 Mon Sep 17 00:00:00 2001 From: 6453330b12700e0fdb30f0d0 <6453330b12700e0fdb30f0d0@devide> Date: Thu, 4 May 2023 06:13:41 +0000 Subject: [PATCH] Auto commit --- excel.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 21 ++++++++++++++++++++- test.py | 11 +++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 excel.py create mode 100644 test.py diff --git a/excel.py b/excel.py new file mode 100644 index 0000000..d288975 --- /dev/null +++ b/excel.py @@ -0,0 +1,56 @@ +import openpyxl + + +# 读取指定单元格的值 +def read_excel(file_path, sheet_name, row, col): + # 打开 Excel 文件 + wb = openpyxl.load_workbook(file_path) + + # 打开指定的 sheet + sheet = wb[sheet_name] + + # 读取指定单元格的值 + cell = sheet.cell(row=row, column=col) + value = cell.value + + # 关闭文件 + wb.close() + + return value + + +# 写入指定单元格的值 +def write_excel(file_path, sheet_name, row, col, value): + # 打开 Excel 文件 + wb = openpyxl.load_workbook(file_path) + + # 打开指定的 sheet + sheet = wb[sheet_name] + + # 写入指定单元格的值 + cell = sheet.cell(row=row, column=col) + cell.value = value + + # 保存文件 + wb.save(file_path) + + # 关闭文件 + wb.close() + + +# 测试代码 +if __name__ == '__main__': + # 读取指定单元格的值 + file_path = 'example.xlsx' + sheet_name = 'Sheet1' + row = 2 + col = 3 + value = read_excel(file_path, sheet_name, row, col) + print(f'第 {row} 行,第 {col} 列的值为:{value}') + + # 写入指定单元格的值 + row = 3 + col = 4 + value = 'Hello World' + write_excel(file_path, sheet_name, row, col, value) + print(f'已将 {value} 写入第 {row} 行,第 {col} 列') diff --git a/main.py b/main.py index 4c0c135..13087c8 100644 --- a/main.py +++ b/main.py @@ -1 +1,20 @@ -print('欢迎来到 InsCode') \ No newline at end of file +# 定义外部函数,接受一个参数x +def outer_func(x): + # 定义内部函数,接受一个参数y + def inner_func(y): + return x + y # 将加号加入计算 + # 返回内部函数 + return inner_func + +# 调用外部函数,将返回的内部函数赋值给变量 +closure_func = outer_func(5) + +# 调用闭包函数,将结果赋值给变量 +result = closure_func(10) + +# 输出结果 +print(result) + + + + diff --git a/test.py b/test.py new file mode 100644 index 0000000..560ccf0 --- /dev/null +++ b/test.py @@ -0,0 +1,11 @@ + +def test_closure_func(): + # 调用外部函数 + closure_func = outer_func(5) + + # 调用闭包函数,将结果与期望值比较 + assert closure_func(10) == 15 + assert closure_func(20) == 25 + assert closure_func(-5) == 0 + +test_closure_func() \ No newline at end of file -- GitLab