Python系列(6)——将三种类型的数据添加进新sheet及已有的sheet中且不覆盖原数据

# -*- coding:utf-8 -*-
import pandas as pd
import openpyxl
import numpy as np
# 有以下三格式的据,将它们添加sheet中以及添加已有的sheet
list1 = [['', '', '未婚', 20], ['李四', '', '已婚', 28], ['', '', '未婚', 18], ['小芳', '', '已婚', 25]]
list2 = {'name': ['a', 'b'], 'age': [18, 20]}
list3 = [{'name': 'tf', 'age': 10}, {'name': 'tom', 'age': 18}]

list4 = [['易烊千', '', '未婚', 20], ['未易', '', '已婚', 25]]
list5 = {'name': ['m', 'h'], 'age': [10, 50]}
list6 = [{'name': 'jerry', 'age': 25}, {'name': 'kk', 'age': 18}]

# --使用to_excel可不使用pd.ExcelWriter文件,直接用openpyxl.load_workbook
# 一、 list1-3如何添加sheet中且不覆盖原
# 1list1据添加sheet
excel_read = pd.ExcelWriter(r'C:\Users\Administrator\Desktop\test4.xlsx', engine='openpyxl')
excel_book = openpyxl.load_workbook(excel_read.path)
work_sheet = excel_book.create_sheet(title='new_list1')   # 增加新sheet
data1 = ['姓名', '', '婚姻', '']   # 放入
work_sheet.append(data1)
for i in list1:
    work_sheet.append(i)
excel_book.save(excel_read)

# 2list2据添加sheet
# 第一种写:使用to_excel--标题是有框的
excel_read = pd.ExcelWriter(r'C:\Users\Administrator\Desktop\test4.xlsx', engine='openpyxl')
# 报错AttributeError: ‘Workbook’ object has no attribute ‘add_worksheet’
# 修改pd.ExcelWriter(xxx, engine=‘openpyxl’)下述其他代码类
excel_book = openpyxl.load_workbook(excel_read.path)
excel_read.book = excel_book   # to_excel时这段代才需要且后面的保存需excel_read.save()
data2 = pd.DataFrame(list2)
data2.to_excel(excel_read, sheet_name='new_list2', index=False)
excel_read.save()

# 第二种写:利用numpy字典据,np.array(..)标题..to_numpy()据,然后分appendsheet--但是这种拆分是有格式
# (但可以置格式)
excel_read = pd.ExcelWriter(r'C:\Users\Administrator\Desktop\test4.xlsx', engine='openpyxl')
excel_book = openpyxl.load_workbook(excel_read.path)
new_sheet = excel_book.create_sheet('new_list2')
data2 = pd.DataFrame(list2)
column = np.array([data2.columns])  # 标题拿出[['name'  'age']]
new_sheet.append(list(column[0]))  # 将标题写进sheet
value = data2.to_numpy()   # 将值拿出[['a' 18] ['b' 20]]
for i in range(len(value)):
    new_sheet.append(list(value[i]))   # 值写sheet
excel_book.save(excel_read)

# 3list3据添加sheet
# 第一种写法:手拆字典,将标题appendsheet
excel_read = pd.ExcelWriter(r'C:\Users\Administrator\Desktop\test4.xlsx', engine='openpyxl')
excel_book = openpyxl.load_workbook(excel_read.path)
work_sheet3 = excel_book.create_sheet(title='new_list3')   # 建新sheet
data3 = [j for j, k in list3[1].items()]   # 入行标题
work_sheet3.append(data3)
for i in list3:
    list_list = []
    for j, k in i.items():
        list_list.append(k)
    work_sheet3.append(list_list)   # 化成列表直接添加到sheet
excel_book.save(excel_read)

# 第二种写法:使用pd.DataFrameto_excel
excel_read.book = excel_book
data3_1 = pd.DataFrame(list3)
data3_1.to_excel(excel_read, sheet_name='new_list3', index=False)
excel_read.save()
# 第三种写法:利用numpy字典据,np.array(..)标题..to_numpy()据,然后分appendsheetlist2的第二种写法一

# 二、list4-6如何添加已存在的sheet中且不覆盖原
# 以下追加新的据到已有sheet中,有使用to_excel,因此也可以不使用pd.ExcelWriter,直接openpyxl.load_workbook(....)
# 1list4据添加已有sheet
excel_read = pd.ExcelWriter(r'C:\Users\Administrator\Desktop\test4.xlsx', engine='openpyxl')
excel_book = openpyxl.load_workbook(excel_read.path)
work_sheet = excel_book['new_list1']
for i in list4:
    work_sheet.append(i)    # 加入到最后一行的下面
excel_book.save(excel_read)

# 2list5据添加已有sheet
excel_read = pd.ExcelWriter(r'C:\Users\Administrator\Desktop\test4.xlsx', engine='openpyxl')
excel_book = openpyxl.load_workbook(excel_read.path)
# excel_read.book = excel_book  # to_excel时这段代才需要,且后面的保存需excel_read.save()
# 若是使用to_excel且后面的保存又excel_read.save()段代excel失去打不,
# 所以若不写这段代,直接保存信息改excel_book.save(excel_read)即可
work_sheet = excel_book['new_list2']
data5 = pd.DataFrame(list5)
value = data5.to_numpy()   # [['m' 10]  ['h' 50]]
for i in range(len(value)):
    work_sheet.append(list(value[i]))   # append容必是可迭代象,里面的valuelist
# 也可使用下面的代
"""
rows = work_sheet.max_row
for i in range(len(value)):
    for j in range(len(value[i])):
        work_sheet.cell(i+1+rows, j+1, value[i][j])
"""
excel_book.save(excel_read)
# 3list6据添加已有sheetlist5型一的方法


# 三、使用openpyxl在已有excel入及追加
def write_xlsx(path, sheetname, value):
    index = len(value)
    # 下面的三建新的excelsheet,若原excel存在被覆盖,不是在已有excel中新建sheet
    # workbook = openpyxl.Workbook()
    # sheet = workbook.active
    # sheet.title = sheetname
    workbook = openpyxl.load_workbook(path)  # 已存在的excel
    sheet = workbook.create_sheet(sheetname)  # excel中新建sheet
    for i in range(index):
        for j in range(len(value[i])):
            sheet.cell(i+1, j+1, str(value[i][j]))   # 为这里是标题,所以要str字符转换
    workbook.save(path)
    print("xlsx格式表格入成功!")


def append_xlsx(path, sheetname, value):
    index = len(value)
    workbook = openpyxl.load_workbook(path)
    sheet = workbook[sheetname]
    """
    rows = sheet.max_row
    for i in range(index):
        for j in range(len(value[i])):
            sheet.cell(i+1+rows, j+1, value[i][j])   # 保持据的原型
                """
    # 也可直接使用下面的代追加
    for i in range(index):
        sheet.append(list(value[i]))  # append容必是可迭代象,里面的valuelist
    workbook.save(path)
    print("xlsx格式表格据追加成!")


df = pd.DataFrame({
        'name': ['Jack', 'kate', 'Jim', 'Steve', 'Mike', 'Lucy'],
        'Gender': ['M', 'F', 'M', 'M', 'M', 'F'],
        'Age': [17, 15, 22, 34, 18, 23],
        'Height': [173, 168, 164, 180, 182, 190],
        'Hobby': ['Reading', 'Music', 'Football', 'Reading', 'Cooking', 'Running']})
df1 = df.copy()
column_name = np.array([df1.columns])
write_xlsx(path=r'C:\Users\Administrator\Desktop\test5.xlsx', sheetname='newsheet', value=column_name)
append_xlsx(path=r'C:\Users\Administrator\Desktop\test5.xlsx', sheetname='newsheet', value=df1.to_numpy())

# 若是不使用def,直接成一串代,可直接成下面的形式
excel_book = openpyxl.load_workbook(r'C:\Users\Administrator\Desktop\test5.xlsx')
new_sheet = excel_book.create_sheet('new_sheet')
data = pd.DataFrame(df)
column = np.array([data.columns])  # 标题拿出[['name'  'Gender' ....]]
new_sheet.append(list(column[0]))  # 将标题写进sheet
value = data.to_numpy()   # 将值拿出[['Jack' 'M' 17 173 'Reading']['kate' 'F' 15 168 'Music'].....]
for i in range(len(value)):
    new_sheet.append(list(value[i]))   # 值写sheet
excel_book.save(r'C:\Users\Administrator\Desktop\test5.xlsx')

# 四、使用xlrd/xlwt在已有excel入和追加
import xlrd  #
import xlwt  #
from xlutils.copy import copy
import pandas as pd
import numpy as np


def write_xls(path, sheetname, value):
    index = len(value)  # 取需要据的行
    # 下面注两条是直接新建excelsheet,若原excel存在被覆盖,不是在已有excel中新建sheet
    # workbook = xlwt.Workbook()  # 建一工作簿
    # sheet = workbook.add_sheet(sheetname)  # 在工作簿中新建一表格
    workbook = xlrd.open_workbook(path, formatting_info=True)  # 工作簿
    new_workbook = copy(wb=workbook)    # 制原excel
    sheet = new_workbook.add_sheet(sheetname, cell_overwrite_ok=True)  # 在工作簿中新建一表格
    for i in range(index):
        for j in range(len(value[i])):
            sheet.write(i, j, value[i][j])  # 向表格中据(对应的行和列)
    new_workbook.save(path)
    print("xls格式表格入成功!")


def append_xls(path, sheetname, value):
    index = len(value)  # 取需要入的据的行
    workbook = xlrd.open_workbook(path, formatting_info=True)  # 工作簿
    # sheets = workbook.sheet_names()  # 取工作簿里的所有表格
    worksheet = workbook.sheet_by_name(sheetname)  # 取工作簿中sheetnamesheet
    rows_old = worksheet.nrows  # 取表格中已存在的据的行
    new_workbook = copy(wb=workbook)  # xlrd象拷贝转xlwt
    new_worksheet = new_workbook.get_sheet(sheetname)  # 化后的sheetxlwtsheet信息:workbook.get_sheet(索引或者'sheet')
    for i in range(index):
        for j in range(len(value[i])):
            new_worksheet.write(i + rows_old, j, value[i][j])  # 追加据,注意 i+rows_old
    new_workbook.save(path)
    print("xls格式表格追加入成功!")


df = pd.DataFrame({
        'name': ['Jack', 'kate', 'Jim', 'Steve', 'Mike', 'Lucy'],
        'Gender': ['M', 'F', 'M', 'M', 'M', 'F'],
        'Age': [17, 15, 22, 34, 18, 23],
        'Height': [173, 168, 164, 180, 182, 190],
        'Hobby': ['Reading', 'Music', 'Football', 'Reading', 'Cooking', 'Running']})
df1 = df.copy()
column_name = np.array([df1.columns])
write_xls(path=r'C:\Users\Administrator\Desktop\test2_xlwt.xls', sheetname='newsheet', value=column_name)
append_xls(path=r'C:\Users\Administrator\Desktop\test2_xlwt.xls', sheetname='newsheet', value=df1.to_numpy())
print(column_name)  # [['name' 'Gender' 'Age' 'Height' 'Hobby']] 标题
print(df1.to_numpy())  # 所有据以列表形式[['Jack' 'M' 17 173 'Reading']['kate' 'F' 15 168 'Music'].....]
# 由于xlwtappend性,因此不能直接new_sheet.append(list(column_name[0])) 需要new_sheet.write(...)一步步,即按照上面的函

 

上一篇:Java-POI 4.1.2 表格导入导出2020.2.17官网发布的库


下一篇:Excel文档操作