我尝试使用pyexcel添加行数据。我一直在阅读文档和食谱,但无济于事。我知道它可能,我可能没有看到明显的。在cookbook中我找到了一个名为update_rows()的方法,它需要三个args(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)
提前致谢。
答案 0 :(得分:5)
我是pyexcel的所有者,并且已更新有关your use case的文档。关于行操作的更多细节可以在api referance Sheet.Row中找到。
以下是添加新行的示例代码的副本:
>>> 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 |
+----------+----------+----------+