I am trying to write multiple sheets into one workbook and I'm using pyexcelerate to utilize it's optimized writing time.

我正在尝试将多个工作表写入一个工作簿,并且我正在使用pyexcelerate来利用它的优化写入时间。

Here's my current code to write to a workbook:

这是我写入工作簿的当前代码:

def df_to_excel(df, path, sheet_name='Sheet 1'):
    data = [df.columns.tolist(), ] + df.values.tolist()
    wb = Workbook()
    wb.new_sheet(sheet_name, data=data)
    wb.save(path)

now this works perfectly fine if i only need one sheet; however, if i write in multiple sheet, only the last sheet will be kept (all sheets generated before will be replaced).

现在,如果我只需要一张纸,这完全没问题。但是,如果我在多张纸上书写,则只保留最后一张纸(之前生成的所有纸张都将被替换)。

I want to keep all the sheets (with diff names ofc), and I looked into their github page, but I cannot find info on such capability: https://github.com/kz26/PyExcelerate

我想保留所有的工作表(使用diff名称为c),我查看了他们的github页面,但我找不到有关此类功能的信息:https://github.com/kz26/PyExcelerate

I also looked into a few other stackoverflow posts but they are using different packages:

我还查看了一些其他stackoverflow帖子,但他们使用不同的包:

  1. Writing resutls into 2 different sheets in the same Excel file
  2. 在同一个Excel文件中将resutls写入2个不同的工作表
  3. xlwt create dynamic number of worksheets based on input
  4. xlwt根据输入创建动态数量的工作表
  5. creating multiple excel worksheets using data in a pandas dataframe
  6. 使用pandas数据框中的数据创建多个excel工作表

any help is appreciated!

任何帮助表示赞赏!

1 个解决方案

#1


0

If you want to save different variable in different sheet of one excel file faster and also want header and index of the pandas.dataframe in the excel file. You can do something as follows,

如果要更快地将不同的变量保存在一个excel文件的不同表中,并且还需要excel文件中pandas.dataframe的标题和索引。你可以做如下的事情,

    import timeit
    from pyexcelerate import Workbook

    def to_Excel(data, fileName):
        start_time = timeit.default_timer()
        wb = Workbook()
        for key in data.keys():
            ws = wb.new_sheet(key)
            frame = data[key]
            frame = frame.transpose()
            frame.reset_index(level=0, inplace = True)
            frame = frame.transpose()
            frame.reset_index(level=0, inplace = True)
            row_num = frame.shape[0]    
            col_num = 1
            for col_name, col_series in frame.iteritems():
                ws.range((1,col_num), (row_num,col_num)).value = [[x] for x in col_series]
                col_num += 1       
        wb.save(fileName)
        end_time = timeit.default_timer()
        delta = round(end_time-start_time,2)
        print("Took "+str(delta)+" secs")

'data' is a dictionary variable. The 'key' of 'data' works as 'sheet name' and data type of each key value should be in pandas dataframe.

'data'是一个字典变量。 'data'的'key'作为'sheet name',每个键值的数据类型应该在pandas数据帧中。

更多相关文章

  1. Python之错误异常和文件处理
  2. python 之 logger日志 字典配置文件
  3. [置顶] Python + C/C++ 嵌入式编程(1):多维数组Numpy.Array(
  4. 用python阐释工作量证明(proof of work)
  5. gsutil - 正则表达式与替代不工作
  6. 使用python api递归计算每个Dropbox文件夹大小
  7. 如何在python中播放wav文件?
  8. python多线程文件传输范例(C/S)
  9. Python - 将值打印到新文件?

随机推荐

  1. 使用倒计时进度条退出
  2. jQuery Slimbox没有正确请求文件
  3. 如何通过javascript显示隐藏的div?
  4. Javascript对象
  5. 在jQuery中使用css transform属性
  6. 根据用户时区显示当地时间 php+javascrip
  7. 从两个数组生成JSON
  8. 使用谷歌地图computeDistanceBetween获取
  9. ajax请求中URL和参数的编码问题
  10. 在KENDO UI Multiselect中选择默认值