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?
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")
Related
I have a very simple spreadsheet with check-marks I want to modify with Python. When I use workbook.save(), the check-marks disappear for some reason.
This is a simplified version of the script I am using, which still reproduces the issue.
from openpyxl import load_workbook
workbook = load_workbook(filename='example.xlsx')
workbook.sheetnames
workbook.active = 0
sheet = workbook.active
sheet
sheet.title
workbook.save(filename="example.xlsx")
This is the spreadsheet before running the script.
This the spreadsheet after running the script.
I have openpyxl 3.0.7 and I don't get any error messages. When I try to install an old version of openpyxl, like pip 3.0.5, and I open the spreadsheet, I get this message.
We found a problem with example.xlsx, we can try to retrieve its content.
I don't know if this information can help.
If you want to open the spreadsheet here you have.
You have to use .xlsm files instead of .xlsx and you have to write:
load_workbook(filename='example.xlsm', read_only=False, keep_vba=True)
instead of:
load_workbook(filename='example.xlsm')
I am trying to add data to an existing excel file, the problem I am facing is that the data is getting imported but the equation and the format is being deleted in original file.
I attached my code below
import xlwt
import xlrd
from xlutils.copy import copy
#open the excel file
rb=xlrd.open_workbook('Voltage_T.xlsx')
#make a writable copy of the opened excel file
wb=copy(rb)
#read the first sheet to write to within the writable copy
w_sheet=wb.get_sheet(0)
#write or modify the value at 2nd row first column
w_sheet.write(0,1,'WWW.GOOGLE.COM')
#the last step saving the work book
wb.save('Voltage_WW.xls')
You need to set formatting_info to true
rb=xlrd.open_workbook('Voltage_T.xlsx', formatting_info = True)
However xlrd doesn't support xlsx with formatting_info at the moment. So if you really have to use .xlsx you will need another library.
I didn't used it myself so I can't tell you if it's a good library but thanks to a quick search on google XlsxWriter seems to answer your needs.
Currently, I am trying to edit an existing excel file using xlwt. I do not want to edit directly on the excel, so I first make a copy.
new_wb = xlutils.copy(file_location)
From this copy, I used the xlwt module to write a new column into the newly copied excel file and save. However, I get an error when I try to copy:
ValueError: row index was 65536, not allowed by .xls format
I am a little confused because the file I duplicate is a xlsx file, not xls. I never use the xls format in my code.
Any guidance would be greatly appreciated.
Try openpyxl instead. It support .xlsx files.
The row limit of .xls files is 65,536. xlsutils might not be supporting .xlsx files.
You can try doing this to see if it works:
from openpyxl import Workbook, load_workbook
wb = load_workbook('filename.xlsx')
wb = Workbook(write_only=True)
.
.
.
(make your edits)
.
.
.
wb.save('new_filename.xlsx')
Short solution for people encountering the same issue with pandas’ DataFrame.to_excel() : if you are saving into a .xls extension, simply change it for .xlsx extension.
I have a .xlsm file as a reference template. I want to update the values of this .xlsm file using python from a .csv file.
template .xlsm ----> Update values using .csv
What has not worked :
I tried using pandas.to_excel method. but the .xlsm file gets corrupted after I write to sheet.
Could someone please point me in the right direction ?
openpyxl supports xlsm file.
from openpyxl import load_workbook
wb2 = load_workbook('test.xlsm', keep_vba=True)
update(wb2, csvfile.csv) # this is where you need to work according to your need.
wb.save('new_document.xlsm')
wb.close()
https://openpyxl.readthedocs.io/en/default/tutorial.html
Maybe to try xlwings, using it something like this?
def update(workbook, csv_file):
ws = workbook.sheets[2]
ws.range('B14').value = 155
from xlwings import Book
wb = Book(r'test.xlsm')
update(wb, csv_file)
wb.save('test1.xlsm')
wb.close()
This is the best tool to update xlsm files since it uses WindowsAPI and macros are triggered in case something is changed. This means, it won't work on Linux.
Of course, update function must do something more meaningful than changing the B14 cell in the 3rd sheet.
For more info, please read http://docs.xlwings.org/en/stable/quickstart.html
I am trying to format an excel document within python that I am creating in the same script. All of the answers I have found have involved loading an existing workbook into python and formatting from there. In my script, I am currently writing the entire unformatted excel sheet, saving the file, then immediately reloading the document in to python to format. This is the only workaround I can find so that I can have an active sheet.
writer=pd.ExcelWriter(file_name, engine='openpyxl')
writer.save()#saving my file
wb=load_workbook(file_name) #reloading file to format
ws=wb.active
ws.column_dimensions['A'].width=33
ws.column_dimensions['B'].width=16
wb.save(file_name)
This works to change aspects such as column width, but I would like a way to format the page without saving and reloading. Is there a way to get around the need for an active sheet when there is no file_name written yet? I want a way to remove line 2 and 3, however that may be.
The object that Pandas is creating in ExcelWriter depends on the "engine" you give it. In this case, you're passing along "openpyxl", so ExcelWriter is making an openpyxl.Workbook() object. You can create a new Workbook in openpyxl using "Workbook()" Like so:
https://openpyxl.readthedocs.io/en/default/tutorial.html#create-a-workbook
It is created with 1 active sheet. Basically:
import openpyxl
wb = openpyxl.Workbook()
ws=wb.active
ws.column_dimensions['A'].width=33
ws.column_dimensions['B'].width=16
wb.save(file_name)
...would do the job
Your title is misleading: you're working in Pandas and dumping to Excel. Pandas does allow some formatting for this but, because it tries to support different Python libraries (openpyxl, xlsxwriter and xlwt) there are restrictions on this.
For full control openpyxl provides support for Pandas' DataFrame objects: http://openpyxl.readthedocs.io/en/latest/pandas.html