用Python对从第4行第6列之后的部分去除空格后,汇总就能与在线文档的一致了
代码如下:
import os
import openpyxl
import re
import warnings
os.chdir(r'E:\一点小忙\办公室\朱小姐')
#读取包含中文名的.xlsx文件时,
#报错“Workbook contains no default style, apply openpyxl’s default ”
file_name = '各项目部管理人员工资办公用品固定资产保险等费用月报表20220530.xlsx'
def read_excel_with_file_name(file_name):
with warnings.catch_warnings(record=True):
file_path = os.path.join('./', file_name)
wb = openpyxl.load_workbook(file_path)
return wb
wb = read_excel_with_file_name(file_name)
for ws in wb:
if ws.title == '2022年' or ws.title == '2021年':
for row in ws.iter_rows(min_row=4,min_col=6):
for cell in row:
value = re.sub(r'\s','',str(cell.value))
if value != 'None' and '+' not in value:
if value == '':
cell.value = None
elif '=' in value:
cell.value = value
else:
try:
cell.value = float(value)
except ValueError:
print(cell)
print(cell.value)
pass
wb_name = 'dataSource-white.xlsx'
wb.save(wb_name)