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)
相关推荐
mykj15513 小时前
英语单词APP解锁学习新方式,智能高效背单词
学习·英语单词app
知识分享小能手3 小时前
深度学习学习教程,从入门到精通,深度学习中的正则化 — 完整知识点与代码示例(7)
人工智能·深度学习·学习
benchmark_cc4 小时前
REST API 和 Python SDK 应该怎么选?量化交易数据接口选型实战
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
~kiss~4 小时前
Agent 的 未来吗?String: An Agentic OS Where Every App Is a Markdown File
学习
HugoStudio_SWAN5 小时前
洛谷 B4450 / B3867 / B3923 智慧购物、储蓄与做题——从程序到生活
c++·学习·程序人生·算法·生活
2601_962382435 小时前
python快乐编程网络爬虫课后答案
python·网络爬虫·学习工具·课后答案·刷题app
鹏哥带你干乡墅6 小时前
优选乡墅赋能培训平台如何帮助提升乡村建设?
大数据·人工智能·python
veminhe6 小时前
python调用接口获取网络信息
python
UIU1147 小时前
scanf与cout的误区:探究其内部的机制
c++·学习·c#·scanf
Allen_LVyingbo7 小时前
医疗AI基础2026-构建可靠智能体的编程路径(上)
大数据·数据库·人工智能·python·自动化