需求:
获取指定的表单2018,去除表单中月份带*的工资。最后计算所有月份的工资。
实现的代码块:
"""
@Project :python
@Author : 文跃锐(yuerwen)
@Time : 2021/11/08
@File :yuerwen_test_去掉包含星号的月份收入.py
"""
'''
那么我们怎么在汇总收入中,去掉包含星号的月份收入呢?
就需要我们查出哪些月份是带星号的,不要统计在内。
'''
import xlrd
# 打开Excel表格 返回book对象
book = xlrd.open_workbook('income.xlsx')
# 获取表单对象
sheet_obj = book.sheet_by_name('2018')
# 获取列的所有数据,返回list
colx_incomes = sheet_obj.col_values(colx=1, start_rowx=1)
print(f'所有工资的总和:{sum(colx_incomes)} 元')
# 算法:去除带*的月份的工资
# 初始指定工资
start_incomes = 0
# 月份在第1列
monthes = sheet_obj.col_values(colx=0)
# 循环获取带*号的数据,存储到指定变量中
for row, month in enumerate(monthes):
# 判断数据是否是字符串并且*结尾
if type(month) is str and month.endswith('*'):
# 获取指定表格中的数据
incomes = sheet_obj.cell_value(row, 1)
print(month, incomes)
# 拿到的数据加到变量中
start_incomes += incomes
print(f"2018年真实收入为: {int(sum(colx_incomes)- start_incomes)} 元")
2021-11-8 23:44 更新
内容:计算Excel中所有表中,去除带*的工资:
代码块:
"""
@Project :python
@Author : 文跃锐(yuerwen)
@Time : 2021/11/08
@File :yuerwen_test_3years.py
"""
import xlrd
book = xlrd.open_workbook('income.xlsx')
sheets = book.sheets()
income_of_3years = 0
for sheet in sheets:
# 获取列的所有数据,返回list
colx_incomes = sheet.col_values(colx=1, start_rowx=1)
print(f'{sheet.name}年所有工资的总和:{sum(colx_incomes)} 元')
# 算法:去除带*的月份的工资
# 初始指定工资
start_incomes = 0
# 月份在第1列
monthes = sheet.col_values(colx=0)
for row, month in enumerate(monthes):
if type(month) is str and month.endswith('*'):
incomes = sheet.cell_value(row, 1)
print(month, incomes)
start_incomes += incomes
print(f"{sheet.name}年去除带*后真实收入为: {int(sum(colx_incomes) - start_incomes)} 元")
income_of_3years += int(sum(colx_incomes) - start_incomes)
print('-----------------------------------------------------------------------')
print(f'全部收入为{income_of_3years}')



