Search code examples
pythonexcelpython-3.4xlrdxlwt

Python append xls file using only xlwt/xlrd


I am having problems appending issues appending data to an xls file. Long story short, I am using a program to get some data from something and writing it in an xls file.

If I run the script 10 times, I would like the results to be appended to the same xls file.

My problem is that I am forced to use Python 3.4 and xlutils is not supported, so I cannot use the copy function.

I just have to use xlwt / xlrd. Note, the file cannot be a xlsx.

Is there any way i can do this?


Solution

  • I would look into using openpyxl, which is supported by Python 3.4. An example of appending to a file can be found https://openpyxl.readthedocs.org/en/default/. Please also see: How to append to an existing excel sheet with XLWT in Python. Here is an example that will do it. Assuming you have an Excel sheet called sample.xlsx:

    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")