Assuming you are on Windows and have Excel installed.
There are multiple libraries to make it possible to use Excel from python. Out of the I only have used win32com.client
but I was pretty content with it. I think it comes with anaconda by default but if not then you can download it from here: https://github.com/mhammond/pywin32/releases (don't forget to select the appropriate version/architecture).
Here is a mini reference to the most important functionalities:
from win32com.client import Dispatch # import the necessary library
xlApp = Dispatch("Excel.Application") # starts excel
xlApp.Visible = False # hides window (this makes things a bit faster)
xlBook = xlApp.Workbooks.Open( fullPathToXlsx ) # opens an existing workbook
xlSheet = xlBook.Sheets(3) # which sheet you want to use, indexing starts from 1!
print [sheet.Name for sheet in xlBook.Sheets] # if it is not the one you want you can see their order this way
xlSheet = xlBook.Sheets(sheetName) # or you can use its name
xlSheet.Cells(row, col).Value = newValue # newValue is what you want to assign to the cell, row and col indexing starts from 1
xlApp.DisplayAlerts = False # turns off any affirmation popup
xlBook.Save() # saving workbook
xlBook.SaveAs(newFullPath) # saving as...
xlBook.Close() # close workbook
xlApp.Quit() # exit application
You might want to convert between column/row index and the 'letter representation (i mean 'A1' for top left cell and so on). Here is what I used for it:
def cellStringToNum(aStr):
"""
returns an excel sheet cell reference as numbers in a list as [col, row]
"""
import string
colS = ""
for i in xrange(len(aStr)):
if aStr[i] in string.ascii_uppercase:
colS += aStr[i]
else:
row = int(aStr[i:])
break
col = 0
for i in xrange(len(colS)):
col += (string.ascii_uppercase.find(colS[len(colS)-1-i])+1)*(26**i)
return [col, row]
def cellNumToString(col, row):
import string
colS = string.ascii_uppercase[col % 26 - 1]
if col % 26 == 0:
col -= 26
else:
col -= col % 26
while col != 0:
col /= 26
colS = string.ascii_uppercase[col % 26 - 1] + colS
if col % 26 == 0:
col -= 26
else:
col -= col % 26
return colS+str(row)
Edit
But this question is already answered here: Python - Write to Excel Spreadsheet