Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用pyexcel python在电子表格中添加行数据_Python - Fatal编程技术网

使用pyexcel python在电子表格中添加行数据

使用pyexcel python在电子表格中添加行数据,python,Python,我正在尝试使用pyexcel添加行数据。我读了一段时间的文档和食谱,但都没有用。我知道这是可能的,但我可能看不到显而易见的东西。在食谱中,我发现了一个名为update_rows()的方法,它需要三个参数(fileToRead、list/dictionary、outputFile) 我收到以下错误:NotImplementedError:我们不覆盖文件 我可以看出这不是我想要的方法。我欢迎任何使用.ods格式的模块,如果它更适合我的需要 我的代码如下: import os import pyexc

我正在尝试使用pyexcel添加行数据。我读了一段时间的文档和食谱,但都没有用。我知道这是可能的,但我可能看不到显而易见的东西。在食谱中,我发现了一个名为update_rows()的方法,它需要三个参数(fileToRead、list/dictionary、outputFile) 我收到以下错误:NotImplementedError:我们不覆盖文件

我可以看出这不是我想要的方法。我欢迎任何使用.ods格式的模块,如果它更适合我的需要

我的代码如下:

import os
import pyexcel
import pyexcel.ext.ods
from pyexcel.cookbook import update_rows
import datetime

def mkExcel(dataList, tDate, pathToFile):

    whereToGo = os.path.join(os.path.expanduser(pathToFile), "Archive_%s.ods") %tDate

    if not os.path.exists(whereToGo):
        dataList = pyexcel.utils.dict_to_array(dataList)
        # "output.xls" "output.xlsx" "output.ods" "output.xlsm"
        dataList = pyexcel.Sheet(dataList)
        print dataList
        dataList.save_as(whereToGo)
    else:
        dSheet = pyexcel.load(whereToGo, name_columns_by_row=0)
        dataList = pyexcel.utils.dict_to_array(dataList)

        custom_row = {"Row -1":[11, 12, 13]}
        ## update_rows(existing.ods, custom_row, new.ods)
        update_rows(dSheet, custom_row, whereToGo)

now = datetime.datetime.now()
now = '%s-%s-%s'%(now.year, now.month, now.day)

example_dict = {"Column 1": [1, 2, 3], "Column 2": [4, 5, 6], "Column 3": [7, 8, 9]}

here = os.getcwd()
mkExcel(example_dict, now, here)

提前感谢。

我是pyexcel的所有者,已经更新了有关的文档。关于行操作的更多细节可以在api参考中找到

下面是添加新行的示例代码副本:

>>> import pyexcel as pe
>>> import pyexcel.ext.xls
>>> sheet = pe.get_sheet(file_name="example.xls")
>>> sheet # just to show what the sheet contains
Sheet Name: pyexcel
+----------+----------+----------+
| Column 1 | Column 2 | Column 3 |
+----------+----------+----------+
| 1        | 4        | 7        |
+----------+----------+----------+
| 2        | 5        | 8        |
+----------+----------+----------+
| 3        | 6        | 9        |
+----------+----------+----------+
>>> sheet.row += [12, 11, 10] # now add it to its row
>>> sheet.save_as("new_example.xls") # save it to a new file
>>> pe.get_sheet(file_name="new_example.xls") # read it back
Sheet Name: pyexcel
+----------+----------+----------+
| Column 1 | Column 2 | Column 3 |
+----------+----------+----------+
| 1        | 4        | 7        |
+----------+----------+----------+
| 2        | 5        | 8        |
+----------+----------+----------+
| 3        | 6        | 9        |
+----------+----------+----------+
| 12       | 11       | 10       |
+----------+----------+----------+

如果你想使用其他库,你可以考虑。

不能要求一个更好的答案。也不是来自比所有者更好的来源。我试着投票支持你的答案,但没有足够的声望。我会尽快的。至于另一个库,我很乐意使用Pyexcel。我相信我最终会和其他人一起玩,但不是为了这个项目。我发现你的lib很容易使用。有案可稽。我知道答案是如此简单,以至于我会因为没有看到它而自责!有趣的是,当我听到我的电话铃声,醒来发现我的问题解决了时,我正在梦想代码。非常感谢,好的。“我的库”用于处理涉及多个excel文件的数据,这些文件通常由不同的文件格式混合(弄乱),并且无意中为涉及单个文件格式的同一作业提供了一个简单的界面。如果您需要使用字体、公式、样式和图像,则不得使用我的库。除此之外,特性请求是受欢迎的。chfw,在哪里添加特性请求?正如在维护键顺序时声明的顺序,而不是字典顺序一样?chfw您的库非常出色,使我的生活和工作轻松了很多,谢谢@这个家伙在pyexcel和openpyxl之间做了一些事情,当我将两者结合使用时,我已经能够完成所有我认为可能完成的事情。功能+格式化,两者兼有。