Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can openpyxl write list data in sheet?

I am using openpyxl to deal with some scientific data, when I was writing the list data to an excel file, I have a problem.

wb = openpyxl.Workbook(optimized_write=Ture)
ws_write = wb.create_sheet(0)

for i in range(row):
   ws_write.append([datalist[i][0]])

wb.save(filename='data.xlsx')

When it is done, seems like that it only can write the list into the first column of the xlsx file. How can I assign the same col of my datalist to the same col of xlsx file in the same sheet? Is there any reference to control the output?

like image 779
GuangWu Avatar asked Mar 30 '15 19:03

GuangWu


People also ask

How do you write a list data sheet in Excel using Python?

You can write any data (lists, strings, numbers etc) to Excel, by first converting it into a Pandas DataFrame and then writing the DataFrame to Excel. To export a Pandas DataFrame as an Excel file (extension: . xlsx, . xls), use the to_excel() method.

How do I write to a cell in openpyxl?

Openpyxl write to a cell There are two basic ways to write to a cell: using a key of a worksheet such as A1 or D3, or using a row and column notation with the cell method. In the example, we write two values to two cells. Here, we assing a numerical value to the A1 cell.

Can openpyxl write XLS files?

Introduction. openpyxl is a Python library to read/write Excel 2010 xlsx/xlsm/xltx/xltm files.

How to write data to an Excel file using openpyxl?

There are multiple ways to write data to an Excel file using Openpyxl. You can also use methods like iter_rows () and iter_cols () to write data to Excel. Openpyxl also provides an append () method. This is used to append values to an existing Excel sheet. Each Excel workbook can contain multiple sheets.

How do I write to an Excel file in Python?

Python | Writing to an excel file using openpyxl module. Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files.

What is openpyxl module in Python?

The Openpyxl module in Python is used to handle Excel files without involving third-party Microsoft application software. Openpyxl is arguably, the best python excel library that allows you to perform various Excel operations and automate excel reports using Python. You can perform all kinds of tasks using Openpyxl like:-

How to print the sheet names in openpyxl?

Note: You can Print the sheet names using print (“active sheet title: ” + sheet.title) In Openpyxl, if you delete a sheet, be a little careful as it is not recoverable. Let’s create an Excel sheet with 3 Sheets and call it as “Sheet1” “Sheet2” “Sheet3” and save it as DeleteSheet.xlsx.


2 Answers

When using ws.append(row) you must insert pass in a whole either as sequence or as a generator. I don't understand your data structure but something like the following should work.

for row in datalist:
    ws.append(row)

If this is not sufficient then please provide more information about the structure of your data.

like image 62
Charlie Clark Avatar answered Sep 29 '22 14:09

Charlie Clark


You are only writing the first column of your data (col=0). In order to include all the data, either add an internal loop:

for i in range(row):
    for j in range(col):
       ws_write.append([datalist[i][j]])
wb.save(filename='data.xlsx')

or, write the entire row at a time:

for i in range(row):
   ws_write.append([datalist[i]])
wb.save(filename='data.xlsx')

I do not know that package to make sure that the output syntax is correct.

edit: after looking at this:Insert row into Excel spreadsheet using openpyxl in Python it appears that you need to do something like:

for i in range(row):
    for j in range(col):
       ws_cell(row=i,col=j).value = datalist[i][j]
wb.save(filename='data.xlsx')
like image 31
Alan Hoover Avatar answered Sep 29 '22 14:09

Alan Hoover