python-批量操作excel

  1. 批量新增excel文件

    python 复制代码
    	import os
    	import xlwings  as xw
    	app = xw.App(visible=True,add_book=False)#visible设置为ture的时候会自动打开创建的excel文件,设为为false的时候不会看到excel文件打开了,实际进程占用了....
    	dept_list = ['人事部','财务部','研发部','行政部']
    	path = os.path.abspath(os.path.abspath(os.path.dirname(__file__)))
    	dept_path = os.path.join(path,'excel文件')
    	
    	for dept in dept_list:
    	    workbook = app.books.add()
    	    workbook.save(f'{dept_path}/部门业绩-{dept}.xlsx')
  2. 批量打开excel文件

    python 复制代码
    import os
    import xlwings  as xw
    app = xw.App(visible=True,add_book=False)
    for dir,curdir,files in os.walk(dept_path):
        if files:
           for  file in files:
                file_path = os.path.join(dir,file)
                if file.endswith('.xlsx'):
                    app.books.open(file_path)
  3. 批量修改excel文件工作表名

    复制代码
     import os
     import xlwings  as xw
     app = xw.App(visible=False,add_book=False)
     path = os.path.abspath(os.path.abspath(os.path.dirname(__file__)))
     dept_path = os.path.join(path,'excel文件')
     #批量修改  【部门业绩-行政部.xlsx  】 这个文件的工作表名
     workbook =app.books.open(f'{dept_path}\部门业绩-行政部.xlsx')
     for sheet in workbook.sheets:
         sheet.name = sheet.name.replace('Sheet','部门')
     workbook.save()
     app.quit()

4.合并相似excel文件内容到一个excel(只会合并每个excel第一个sheet页的内容)

python 复制代码
	import pandas as pd
	data_list = []
	for dir,curdir,files in os.walk(dept_path):
	    if files:
	       for  file in files:
	            file_path = os.path.join(dir,file)
	            if file.endswith('.xlsx') and file.startswith('部门业绩-'):
	                data_list.append(pd.read_excel(file_path))
	
	data_all = pd.concat(data_list)
	data_all.to_excel('部门业绩.xlsx')
  1. 合并一个excel文件多个sheet页内容到第一个sheet页中

    python 复制代码
    import pandas as pd
    import xlwings as xw
    file = f'{dept_path}\部门业绩-行政部.xlsx'
    df_list = pd.read_excel(file,sheet_name=None)
    df_all = pd.concat(df_list.values())
    app=xw.App(visible=False,add_book=False)
    workbook = app.books.open(file)
    workbook.sheets.add('汇总表',before=workbook.sheets[0])
    workbook.sheets['汇总表'].range('A1').options(index=False).value=df_all
    workbook.save()
    workbook.close()
    app.quit()
  2. 拆分excel表(按照某个分类拆分成不同的excel表)

    python 复制代码
    import pandas as pd
    file = f'{dept_path}\产品表.xlsx'
    df= pd.read_excel(file)
    products = df['产品类型'].unique()  #去重获取产品类型这一列所有产品
    
    for product in products:
        df_product = df[df['产品类型']==product]
        df_product.to_excel(f'{dept_path}\产品表-{product}.xlsx')
  3. 拆分excel表(按照某个分类拆分成不同的sheet页)

    python 复制代码
    import pandas as pd
    file = f'{dept_path}\采购表.xlsx'
    #读取所有sheet页
    df_list= pd.read_excel(file,sheet_name=None,parse_dates=False)
    #合并成一个大数据表
    df_all = pd.concat(df_list.values())
    
    file2 = f'{dept_path}\采购表-按产品类分.xlsx'
    excel_writer = pd.ExcelWriter(file2,date_format='YYYY-MM-DD')
    for product,df in df_all.groupby('产品类型'):
        df.to_excel(excel_writer,product,index=False)
    excel_writer.save()
  4. 比较2个excel文件内容

    python 复制代码
    import os
    import xlwings  as xw
    
    app = xw.App(visible=False,add_book=False)
    file = f'{dept_path}\产品表.xlsx'
    file_back = f'{dept_path}\产品表-备份.xlsx'
    book = app.books.open(file)
    book_backup = app.books.open(file_back)
    
    for row in book.sheets[0].range('A1').expand():
        for cell in row:
            backup_cell = book_backup.sheets[0].range(cell.address)
            if cell.value != backup_cell.value:
                cell.color = backup_cell.color = (255,0,0)
    book.save()
    book.close()
    book_backup.save()
    book_backup.close()
    app.quit()
相关推荐
田里的水稻4 小时前
PyTorch_Dataloader\TensorBoard---数据加载与可视化
人工智能·pytorch·python·神经网络·机器学习
Dmz丶4 小时前
前端转 AI 100 天 Day 27:Docker 容器化——从“我电脑能跑“到“到处能跑“
python
卿卿的产品经理日记4 小时前
【AI产品经理实战】Day 38|try-except 踩坑实录:报错是数据
人工智能·笔记·python·学习·产品经理
全栈练习生4 小时前
文档智能与实践
python·ai
爱喝热水的呀哈喽4 小时前
HEJIAN RESEARCH
开发语言·matlab
白露与泡影4 小时前
后端 Java 面试核心八股汇总|高频面试题 + 解析,覆盖基础到进阶
java·开发语言·面试
xifangge20254 小时前
基于 JavaScript 实现 CNC 数控切削参数与螺纹公差纯前端核算引擎
开发语言·前端·javascript
隐擎fox4 小时前
深入 Android 移动端风控与协议层对抗:内核级虚拟环境检测、网络堆栈特征伪装与住宅 IP 协同实战
android·网络·python·网络协议·tcp/ip
“AI国潮设计-小江”5 小时前
【AIGC实战】Python+SDXL批量生成「潮汕英歌舞冰淇淋蛋糕&红桃粿咖啡」IP,固定角色形象批量出甜品商业效果图
开发语言·人工智能·python·prompt·aigc
\光辉岁月/5 小时前
3.包装类
java·开发语言·包装类