【创意、创造】用 Python pandas做一个读取Excel,再写入新的Excel小工具
共 3261字,需浏览 7分钟
·
2026-04-16 11:12
Python很好很强大,1.5天时间,简化很多重复的劳动,哈哈~
import pandas as pd
import datetime as dt
def handleFrontEnd(frontIO):
# 处理【上周前端发版】开始
sheet_front_end = pd.read_excel(frontIO, sheet_name='Sheet1', header=None)
# columns=['时间','项目', '人员', '团队', '发布原因', '是否记为Bug']
col_project = 0
col_staff = 2
sheet_front_end.dropna(inplace=True)
print(sheet_front_end)
for x in sheet_front_end.index:
if (sheet_front_end.loc[x, col_project] == '项目'
or 'xxx-' in sheet_front_end.loc[x, col_project]
or 'xxxxx-' in sheet_front_end.loc[x, col_project]):
sheet_front_end.drop(x, inplace=True)
continue
sheet_front_end.loc[x, col_staff] = sheet_front_end.loc[x, col_staff].strip()
sheet_front_end.sort_values(by=col_staff, ascending=True, inplace=True) # 很方便地调整这几列前后顺序
cols = sheet_front_end.columns[[1, 0, 2]]
sheet_front_end = sheet_front_end[cols]
df1 = pd.DataFrame(sheet_front_end)
df1.to_excel('D:\上周前端发版 new.xlsx', index=False, header=None)
# 处理【上周前端发版】结束
def handleBackEnd(backIO):
# 处理【上周后端发版】开始
sheet_back_end = pd.read_excel(backIO, sheet_name='Sheet1', header=None)
sheet_back_end.dropna(inplace=True)
for x in sheet_back_end.index:
sheet_back_end.loc[x, 2] = sheet_back_end.loc[x, 2].replace('Z', '').replace('T', ' ')
if ('xxx-' in sheet_back_end.loc[x, 1]
or 'xxxxx-' in sheet_back_end.loc[x, 1]):
sheet_back_end.drop(x, inplace=True)
continue
# 生产发布时间,转为北京时间,时区+8。
original_time = dt.datetime.strptime(sheet_back_end.loc[x, 2], "%Y-%m-%d %H:%M:%S")
sheet_back_end.loc[x, 3] = (original_time + dt.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S")
# 容器
gang_num = sheet_back_end.loc[x, 1].count('-')
pot_instance_name = sheet_back_end.loc[x, 1]
if gang_num - 2 <= 0:
sheet_back_end.loc[x, 4] = pot_instance_name
else:
tmp_col4_val = pot_instance_name[0:-16]
if tmp_col4_val[-1] == '-':
tmp_col4_val = tmp_col4_val[0:-1]
sheet_back_end.loc[x, 4] = tmp_col4_val
# owner,某所属人原表格文件,【被查找字段】注意依照【升序排序】。否则,VLOOKUP数据不准。
sheet_back_end.loc[x, 5] = '=VLOOKUP($E2,某所属人原表格文件!$D:$J,5)'
# owner确认(是/否)
sheet_back_end.loc[x, 6] = '=IF(VLOOKUP($E2,某所属人原表格文件!$D:$J,6)=0,"",VLOOKUP($E2,某所属人原表格文件!$D:$J,6))'
# 使用情况
sheet_back_end.loc[x, 7] = '=IF(VLOOKUP($E2,某所属人原表格文件!$D:$J,7)=0,"",VLOOKUP($E2,某所属人原表格文件!$D:$J,7))'
sheet_back_end.loc[x, 8] = ''
sheet_back_end.loc[x, 9] = ''
sheet_back_end.loc[len(sheet_back_end.index)] = ['原表格字段1', '原表格字段2', '原表格字段3', '生产发布时间', '容器', 'owner',
'owner确认(是/否)', '使用情况', '发布原因(含重启)', '是否记为Bug']
sheet_back_end.sort_values(by=3, ascending=False, inplace=True)
print(sheet_back_end)
df2 = pd.DataFrame(sheet_back_end)
df2.to_excel('D:\上周后端发版 new.xlsx', index=False, header=None)
# 处理【上周后端发版】结束
frontIO = 'D:\某前端原文件.xlsx'
handleFrontEnd(frontIO)backIO = 'D:\某后端原文件.xlsx'
handleBackEnd(backIO)评论
