Python write a list to multiple columns in excel

0 votes

I'm attempting to create numerous columns in Excel. I have a list that looks like this.

list1=[1,2,3,4,5]

the other list will be like

list2=['a','b','c']

I can't make a dataframe in pandas and write it since I will have several varied sized(length) lists.

list3=['1a','2b','3c','4d']

I want it in excel be like

enter image description here

I am not sure which framework to use, openpyxl or pandas which will solve this issue

I have used your suggestion in this way;

with pd.ExcelWriter(var_path, engine='openpyxl', mode='r+', if_sheet_exists='overlay') as writer:
    book = load_workbook(var_path)
    current_sheet = book[var_sheetname]
    Column_H = current_sheet['H']
    try:
        maxrow = max(c.row for c in Column_H if c.value is not None)
    except ValueError:
        maxrow = 1

    print(var_sheetname,maxrow,'writing docs')
    var_inp.to_excel(writer,startrow=maxrow,columns=Column_H,sheet_name=var_sheetname,index=False,header=False
Mar 30, 2023 in Others by Kithuzzz
• 38,000 points
4,164 views

1 answer to this question.

0 votes

Make a nested list :

L = [list1, list2, list3]

Then you can use Worksheet.append :

from openpyxl import Workbook

wb = Workbook()
ws = wb.active

for sub_list in L:
    ws.append(sub_list)

wb.save("book1.xlsx")
    

Or with pandas :

import pandas as pd

pd.DataFrame(L).to_excel("book2.xlsx", header=False, index=False)

Output (spreadsheet) :

enter image description here

answered Mar 30, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

How to create a drop-down list in Excel?

Making a list of the items you ...READ MORE

answered Oct 9, 2022 in Others by narikkadan
• 63,600 points
753 views
0 votes
1 answer

Create unique rows in Excel with limited data to be used in multiple columns

This setup isn't readily generalizable, though since ...READ MORE

answered Oct 14, 2022 in Others by narikkadan
• 63,600 points
756 views
0 votes
1 answer

Convert table in a jpg image to excel using python

I believe you must execute OCR (optical ...READ MORE

answered Oct 16, 2022 in Others by narikkadan
• 63,600 points
2,301 views
0 votes
1 answer

How can I use a command button in excel to set the value of multiple cells in one click?

Try this: Private Scan As Integer Private Sub CommandButton1_Click() ...READ MORE

answered Oct 24, 2022 in Others by narikkadan
• 63,600 points
798 views
0 votes
1 answer

ImportError: openpyxl is required for loading excel format files

Forget the PsychoPy complications for the time ...READ MORE

answered Oct 3, 2018 in Python by Priyaj
• 58,020 points
1,126 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,470 views
0 votes
1 answer

Is there any way in python to auto-correct spelling mistake in multiple rows of an excel files of a single column?

Use Spellchecker for doing your stuff: import pandas ...READ MORE

answered Oct 14, 2022 in Others by narikkadan
• 63,600 points
2,085 views
0 votes
1 answer

Is there a function to unhide columns in excel through python

Excel file : df.to_excel('demofile.xlsx',index=False) import openpyxl py = openpyxl.load_workbook('demofile.xlsx') exlsheet = ...READ MORE

answered Oct 18, 2022 in Others by narikkadan
• 63,600 points
2,302 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP