python学习 21 excel分列

简单做了个模板,能干这个:

指定sheet中某列数据进行按需拆分,拆分后新建一个test的sheet,将数据分别存入test的不同列中。

python 复制代码
import os
import xlwings as  xw
import re
#excel路径
path=r'C:\Users\xxx\Desktop\123.xlsx'

#设置需要分列的sheet名
sheetname='Sheet1'
#设置需要分列的列名
colname='B'
#开始单元格
rowb=2
#结束单元格
rowe=25

def excel分列(path):
    excelapp=xw.App(visible=False, add_book=False)
    #打开excel
    excel = excelapp.books.open(path)
    #获取sheets
    sheet = excel.sheets
    #增加一个拆分用sheet,test
    if sheet[0].name !='test':
        sheet.add('test', before=sheet[0].name)
    #print(sheet[0].name)
    sheet2 = excel.sheets['Sheet1']

    #设置拆分条件,这里以复合字串为例,型如:你好123+好的234,拆成:你好,123,+,好的234
    num1=re.compile('\w.[0-9]')
    #print(re.findall(num1,sheet2.range('b2:b25').value[0])[0][:-1])
    num2=re.compile('\d++')
    #print(re.findall(num2,sheet2.range('b2:b25').value[0])[0])
    num3=sheet2.range('b2:b25').value[0].split('+')
    #print(num3[1])

    t1=[]
    t2=[]
    t3=[]
    t4=[]

    #拆分取值
    for i in sheet2.range(colname+str(rowb)+':'+colname+str(rowe)).value:
        t1.append(re.findall(num1,i)[0][:-1])
        t2.append(re.findall(num2,i)[0])
        t3.append('+')
        t4.append(i.split('+')[1])
    #print(t1,t2,t3,t4)
    #将拆分数据填入test中
    sheet3=excel.sheets['test']
    for i in range(len(t1)):
        sheet3.range('A'+str(i+1)).value=t1[i]
        sheet3.range('B' + str(i + 1)).value = t2[i]
        sheet3.range('C' + str(i + 1)).value = t3[i]
        sheet3.range('D' + str(i + 1)).value = t4[i]



    excel.save()
    excel.close()
    excelapp.quit()

excel分列(path)
相关推荐
真的想上岸啊23 分钟前
学习C++、QT---18(C++ 记事本项目的stylesheet)
开发语言·c++·学习
摸爬滚打李上进34 分钟前
重生学AI第十六集:线性层nn.Linear
人工智能·pytorch·python·神经网络·机器学习
rui锐rui1 小时前
大数据学习2:HIve
大数据·hive·学习
凛铄linshuo2 小时前
爬虫简单实操2——以贴吧为例爬取“某吧”前10页的网页代码
爬虫·python·学习
牛客企业服务2 小时前
2025年AI面试推荐榜单,数字化招聘转型优选
人工智能·python·算法·面试·职场和发展·金融·求职招聘
胡斌附体2 小时前
linux测试端口是否可被外部访问
linux·运维·服务器·python·测试·端口测试·临时服务器
大春儿的试验田3 小时前
高并发收藏功能设计:Redis异步同步与定时补偿机制详解
java·数据库·redis·学习·缓存
likeGhee3 小时前
python缓存装饰器实现方案
开发语言·python·缓存
项目題供诗3 小时前
黑马python(二十五)
开发语言·python
读书点滴3 小时前
笨方法学python -练习14
java·前端·python