python处理Excel

python处理Excel_第1张图片

python处理Excel_第2张图片

# 批量创建Excel
import  xlwings
app = xlwings.App(visible=True, add_book=False)
for language in ['Java', 'Python', 'C#', 'Vue', "JavaScript"]:
    workbook = app.books.add()
    workbook.save(f"./畅销开发语言--{language}.xlsx")
# 批量打开Excel
import os
import  xlwings as xw
app = xw.App(visible=True,add_book=False)
# os.listdir(path)  列出指定目录下的内容
for file in  os.listdir("."):
    if file.endswith('.xlsx') or file.endswith('.xlsx'):
        app.books.open(file)
        
# 批量重命名工作表
import xlwings as xw
app = xw.App(visible=False, add_book=False)
workbook = app.books.open("畅销开发语言--Python.xlsx")
for sheet in workbook.sheets:
    sheet.name = sheet.name.replace('Sheet', 'ZEN')
workbook.save()
app.quit()


#  合并Excel文件
import pandas as pd
import os

data_list = []
for filename in os.listdir('.'):
    if filename.startswith('畅销开发语言--') and filename.endswith('.xlsx'):
    	# pd.read_excel("xx.xlsx", sheet_name=None)  
    	# sheet_name 默认值0 ,也就是默认打开Excel表中第一个工作簿
        data_list.append(pd.read_excel(filename))
   
data_all = pd.concat(data_list)
data_all.to_excel("合并表.xlsx", index=False)
# 把一个Excel的所有工作表合并,且结果插入第一个位置
# 只是把其它sheet表复制到首个,并没有汇总。
import pandas as pd
import os
import  xlwings as xw

# pd.read_excel("xx.xlsx", sheet_name=None)
# sheet_name 默认值0 ,也就是默认打开Excel表中第一个工作簿
# sheet_name=None 打开所有sheet 工作簿
data_list = pd.read_excel("xxx.xlsx", sheet_name=None)
print(data_list)
data_all = pd.concat(data_list.values())

app = xw.App(visible=False, add_book=False)

workbook = app.books.open("xxx.xlsx")
workbook.sheets.add("汇总表", before=workbook.sheets[0])
workbook.sheets["汇总表"].range("A1").options(index=False).value = data_all

workbook.save()
workbook.close()
app.quit()
#  把Excel 工作表 拆分多个Excel文件
import pandas as pd
data_list = pd.read_excel("xxx.xlsx", sheet_name=0)
courses = data_list["course"].unique()
for course in courses:
    data_single = data_list[data_list["course"] == course]
    data_single.to_excel(f"拆分数据-{course}.xlsx")

你可能感兴趣的:(python,excel,开发语言)