废话不多说直接上代码。
import xlrd
import xlwt
workbook = xlrd.open_workbook(r'C:\Users\input.xls')
sheet1 = workbook.sheet_by_index(0) #对第一个表进行操作
column_index = 6 #匹配对应列
# 创建新的工作簿和工作表
output_workbook = xlwt.Workbook()
output_sheet = output_workbook.add_sheet('Sheet1')
# 处理数据并写入新工作表
for row_index in range(sheet1.nrows):
cell_value = sheet1.cell_value(row_index, column_index)
data = str(cell_value) # 将数据转换为字符串,以确保后续操作正确
index = 0
for i, char in enumerate(data):
if char.isdigit():
index = i
break
name = data[:index]
contact = data[index:]
# 写入数据到新工作表
output_sheet.write(row_index, 0, name)
output_sheet.write(row_index, 1, contact)
# 保存新工作簿为 Excel 文件
output_workbook.save('output.xls')
这里是对Excel文件的第一个表的第六列数据进行处理,将文字和数字切割,单独保存输出为excel文件,根据需求改进循环即可。