I am having some trouble finding the best way to write a list to a loaded excel sheet and then saving the result as an xlsx. What I want my code to do is to take the lists that I have created and put them in specific columns of the loaded workbook. I think there is probably an easier way to do this using dataframes, but I do not know how. Ideally, I would like to save the formatting of the loaded workbook.
col_test = [1L, 2L, 3L, 4L, 5L]
My code is this
import xlrt
from xlrd import open_workbook
rb = open_workbook("Excel FDT Master_01_update.xlsx")
s = rb.sheet_by_name('INPUT')
r = 5
for test in col_test:
s.cell(row = r, column = 1).value = test
r += 1
rb.save('didthiswork.xlsx')
Here is one version with no extra installs on top of anaconda. It is not keeping styling, but that you can fix with a copy/'paste values' back to original xlsx.
Most excel manipulators have issues with keeping the original file intact. There are ways around that as well, but if you want it to be waterproof, you basically end up with a specific solution for you, or more or less recode all libraries out there, so it's not worth the effort.
Pandas can be a bit tricky to get right when extending existing dataframes, but there are always several alternative ways to do it. Here it's done with assign, so then one only needs to make sure that the dataframe's rowcount is long enough for what one wants to add.
The printouts:
That error means that Python could not find a parameter named
row
in the definition ofcell
. If I'm not mistaken this is the xlrd module. Let's take a look at the API documentation forSheet.cell()
which you are attempting to call here.It appears that you've simply misnamed the parameters. Changing the line to the following should fix this.
It goes without saying that Python cannot make guesses as to what you meant to type, so whenever you get an error about something not existing when you're sure it does, like the parameter names here, make sure to read the documentation and check for typos. Also, in the future post all relevant info you can find, such as the function definition and the names of the modules you are using.