Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to row using openpyxl?

I am trying to write a list to a row in Python using openpyxl, but to no avail.

The list contains say for example ten values. I need to open an existing worksheet, and write those ten values to their own cell, along one row.

I need to use openpyxl due to its functionality of overwriting existing worksheets compared to xlsxwriter where you can only create new worksheets.

like image 708
eugene Avatar asked Feb 09 '23 11:02

eugene


1 Answers

Have a look here, scroll down to the heading Writing Values to Cells.

TLDR:

>>> import openpyxl
>>> wb = openpyxl.Workbook()
>>> sheet = wb['Sheet']
>>> sheet['A1'] = 'Hello world!'
>>> sheet['A1'].value
'Hello world!

or if you prefer

sheet.cell(row=2, column=3).value = 'hello world'

Update: changed to wb['Sheet'] syntax as per @charlieclark comment, thx

Update: To write mylist into row 2

for col, val in enumerate(mylist, start=1):
    sheet.cell(row=2, column=col).value = val
like image 113
Steve Avatar answered Feb 11 '23 01:02

Steve