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
5,288 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
• 86,360 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
• 86,360 points
1,194 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
• 86,360 points
1,334 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
• 86,360 points
2,856 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
• 86,360 points
1,342 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,877 views
0 votes
2 answers