简体   繁体   English

Python仅使用xlwt / xlrd附加xls文件

[英]Python append xls file using only xlwt/xlrd

I am having problems appending issues appending data to an xls file. 我在将数据附加到xls文件中时遇到问题。 Long story short, I am using a program to get some data from something and writing it in an xls file. 长话短说,我正在使用一个程序从某物获取一些数据并将其写入xls文件。

If I run the script 10 times, I would like the results to be appended to the same xls file. 如果我运行脚本10次,我希望将结果附加到相同的xls文件中。

My problem is that I am forced to use Python 3.4 and xlutils is not supported, so I cannot use the copy function. 我的问题是我被迫使用Python 3.4,并且不支持xlutils ,因此无法使用复制功能。

I just have to use xlwt / xlrd . 我只需要使用xlwt / xlrd Note, the file cannot be a xlsx . 注意,文件不能是xlsx

Is there any way i can do this? 有什么办法可以做到吗?

I would look into using openpyxl, which is supported by Python 3.4. 我会研究使用Python 3.4支持的openpyxl。 An example of appending to a file can be found https://openpyxl.readthedocs.org/en/default/ . 可以在https://openpyxl.readthedocs.org/en/default/中找到附加到文件的示例。 Please also see: How to append to an existing excel sheet with XLWT in Python . 另请参见: 如何使用XLWT在Python中附加到现有的Excel工作表 Here is an example that will do it. 这是一个可以做到的例子。 Assuming you have an Excel sheet called sample.xlsx: 假设您有一个名为sample.xlsx的Excel工作表:

from openpyxl import Workbook, load_workbook


# grab the active worksheet
wb = load_workbook("sample.xlsx")
ws = wb.active
ws.append([3])

# Save the file
wb.save("sample.xlsx")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM