lunes, 8 de diciembre de 2008

Python con Excel

Primero descargamos e instalamos la extensión PyWin32, para ello haga clic aquí.


En el IDLE de Python escriba y ejecute este primer código ejemplo:

from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlApp.Visible = 1
xlApp.Workbooks.Add()
xlApp.ActiveSheet.Cells(1,1).Value = 'Bienvenido a Python'
xlApp.ActiveWorkbook.ActiveSheet.Cells(2,1).Value = 'Hola Mundo'

Si quieren un ejemplo más avanzado:

from win32com.client import Dispatch
mensaje=raw_input("Escribe un mensaje:")
xlApp = Dispatch("Excel.Application")
xlApp.Visible = 1
xlApp.Workbooks.Add()
xlApp.ActiveSheet.Cells(1,1).Value = 10.75
xlApp.ActiveWorkbook.ActiveSheet.Cells(2,1).Value = 12.25
xlApp.ActiveWorkbook.ActiveSheet.Cells(3,1).Formula = "=A1+A2"
xlApp.ActiveWorkbook.ActiveSheet.Cells(4,1).Formula = "=NOW()"
xlApp.ActiveWorkbook.ActiveSheet.Cells(5,1).Formula = "=SUM(A1:A2)"
xlApp.ActiveSheet.Cells(1,1).Font.Size = 14
xlApp.ActiveWorkbook.ActiveSheet.Cells(2,1).Font.Bold=1
xlApp.ActiveSheet.Cells(6,1).Value = mensaje
xlApp.ActiveWorkbook.ActiveSheet.Cells(1,1).EntireColumn.Autofit()



0 comentarios: