简体   繁体   中英

Writing a list to new excel xlsx with dataframes

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

That error means that Python could not find a parameter named row in the definition of cell . If I'm not mistaken this is the xlrd module. Let's take a look at the API documentation for Sheet.cell() which you are attempting to call here.

cell(rowx, colx)

Cell object in the given row and column.

It appears that you've simply misnamed the parameters. Changing the line to the following should fix this.

s.cell(rowx = r, colx = 1).value = test

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.

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.

import pandas as pd

# read the excel
df = pd.read_excel('Geen titel 1.xlsx') # there are options to choose sheet
print('original df')
print(df)
# your data is not valid python syntax so let's assume it's strings
col_test = ['1L', '2L', '3L', '4L', '5L']
new_idx = range(max(len(col_test), len(df.index)))
df = df.reindex(new_idx) # now it will accommodate different lengths
print('reindexed df')
print(df)
df = df.assign(new_col=col_test) # new column added at right side
print('modified df')
print(df)
df.to_excel('the_new.xlsx')

The printouts:

original df
   a  b
0  1  c
1  2  d
2  3  e
reindexed df
     a    b
0  1.0    c
1  2.0    d
2  3.0    e
3  NaN  NaN
4  NaN  NaN
modified df
     a    b new_col
0  1.0    c      1L
1  2.0    d      2L
2  3.0    e      3L
3  NaN  NaN      4L
4  NaN  NaN      5L

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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