vorhandene excel datei in Python mit Xlwt Schreiben

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
lyva
User
Beiträge: 2
Registriert: Mittwoch 27. Juli 2011, 08:53

Hallo,
ich möchte in Python in eine existierende excel Datei Messwerte schreiben. meine Programm sieht wie folgt aus:

Code: Alles auswählen

cTH.Print('Excel Tabelle Write Zelle ' + sFile + ' ' + sTable + ' ' + str(iColumn) + ' ' + str(iLine) + ' ' + sCell, 'INFO')


import xlwt
from xlwt import workbook

try:
   book = xlwt.workbook(sFile)
   sheet1 = book.get_sheet(sTable)
   sheet1.write(iLine, iColumn,sCell)
   book.save(sFile)
   iResult = 1

    #book = Workbook(sFile)
    #sheet = book.get_sheet(sTable)
                        
    #sheet.write(iLine, iColumn, sCell)
    #book.save("sFile")
    #iResult = 1
     
except:
    Report.createSubTest('Excel Tabelle Write Zelle' , 0 )
    Report.createCommentSubTest(' Fehler beim Schreiben von ' + sFile + ' ' + sTable + ' ' + str(iColumn) + ' ' + str(iLine) + ' ' + sCell)
    iResult = 0
    raise
jedoch beim durchführung erscheint folgen Fehler:

Code: Alles auswählen

2011-07-28 14:36:27 MainThread !!! exception-name: AttributeError
2011-07-28 14:36:27 MainThread !!! exception-arguments: 'module' object has no attribute 'workbook'
2011-07-28 14:36:27 MainThread !!! exception-traceback:   File "D:\EXAM\Programme\EXAM\plugins\de.exam.prunner_3.0.1.201105230923\pRunner\loop.py", line 148, in commandLoop
2011-07-28 14:36:27 MainThread                 command: exec(execFileName + '.main()')
2011-07-28 14:36:27 MainThread !!! exception-traceback:   File "<string>", line 1, in <module>
2011-07-28 14:36:27 MainThread                 command: 
2011-07-28 14:36:27 MainThread                 command: testFlow('', '00000000-0000-0000-0000-000000000000')
2011-07-28 14:36:27 MainThread                 command: iResult = _0._e.UmlClass_a29a2d3c1051729de04400144f6890e0.getInstance().Excel_Tabelle_Write_Zelle_4(sFile = 'C:/TEMP/Messwer_Test.xls', sTable =
 'Sheet4', iColumn = 0, iLine = 0, sCell = 'TEST_01')
2011-07-28 14:36:27 MainThread                 command: book = xlwt.workbook(sFile)


mit dem folgende programme Stück

Code: Alles auswählen

cTH.Print('Excel Tabelle Write Zelle ' + sFile + ' ' + sTable + ' ' + str(iColumn) + ' ' + str(iLine) + ' ' + sCell, 'INFO')


import xlwt
from MSExcel import Workbook

try:
   book = workbook(sFile)
   sheet1 = book.get_sheet(sTable)
   sheet1.write(iLine, iColumn,sCell)
   book.save(sFile)
   iResult = 1                 
      
except:
    Report.createSubTest('Excel Tabelle Write Zelle' , 0 )
    Report.createCommentSubTest(' Fehler beim Schreiben von ' + sFile + ' ' + sTable + ' ' + str(iColumn) + ' ' + str(iLine) + ' ' + sCell)
    iResult = 0
    raise
bekommt ich folgende Fehlermeldung:

Fehlermeldung in diese Art:

Code: Alles auswählen

2011-07-28 14:26:19 MainThread !!! exception-name: ImportError
2011-07-28 14:26:19 MainThread !!! exception-arguments: cannot import name workbook
2011-07-28 14:26:19 MainThread !!! exception-traceback:   File "D:\EXAM\Programme\EXAM\plugins\de.exam.prunner_3.0.1.201105230923\pRunner\loop.py", line 148, in commandLoop
2011-07-28 14:26:19 MainThread                 command: exec(execFileName + '.main()')
2011-07-28 14:26:19 MainThread !!! exception-traceback:   File "<string>", line 1, in <module>
2011-07-28 14:26:19 MainThread                 command: 
2011-07-28 14:26:19 MainThread                 command: testFlow('', '00000000-0000-0000-0000-000000000000')
2011-07-28 14:26:19 MainThread                 command: iResult = _0._e.UmlClass_a29a2d3c1051729de04400144f6890e0.getInstance().Excel_Tabelle_Write_Zelle_4(sFile = 'C:/TEMP/Messwer_Test.xls', sTable =
 'Sheet4', iColumn = 0, iLine = 0, sCell = 'TEST_01')
2011-07-28 14:26:19 MainThread    command: from xlwt import workbook 

was mache ich falsch?
kann jemand mir dabei helfen Bitte

Grüße
Lyva
Zuletzt geändert von Anonymous am Freitag 29. Juli 2011, 10:02, insgesamt 1-mal geändert.
Grund: Quelltext in Python-Code-Tags gesetzt.
deets

Fuer den Anfang, setzt deinen Source bitte in python-tags, damit man ueberhaupt was lesen kann.
BlackJack

@lyva: Du hast nicht zufällig ein eigenes Modul `xlwt` genannt? Lass Dir mal nach dem ``import xlwt`` das Objekt ausgeben, dass an den Namen gebunden ist.
lyva
User
Beiträge: 2
Registriert: Mittwoch 27. Juli 2011, 08:53

Code: Alles auswählen

ich habe das Programm angepasst,ich kann jetzt zumindest mit Python eine Wert in excel  schreiben. jedoch während der Messung wird nur den Letzten Wert in die Tabelle gespeichert. wie soll ich das Programm anpassen damit bei der Messung alle werte gespeichert werden?
die Python funktion sieht zurzeit wie folgt aus

" description: Write Excel files with Python using xlwt"

# log Messgae

cTH.Print('Excel Tabelle Write Zelle ' +sFile + ' ' + sTable + ' ' + str(iColumn) + ' ' + str(iLine) + ' ' + sCell, 'INFO')

# initialize a Workbook object and add a Worksheet object to that Workbook
import xlwt
from xlwt import BIFFRecords
from xlwt import Formatting
from xlwt import Style
from xlwt import UnicodeUtils

try:

     book = xlwt.Workbook() 
     sheet = book.add_sheet(sTable) 
     #shett = book.get_sheet(sTable)
     
     # write Data to Worbook

     sheet.write(iLine,iColumn,sCell)
     
     # save the workbook
     
     book.save(sFile) 
     iResult = 1
except:
    Report.createSubTest('Excel Tabelle Write Zelle' , 0 )
    Report.createCommentSubTest(' Fehler beim Schreiben von ' + sFile + ' ' + sTable + ' ' + str(iColumn) + ' ' + str(iLine) + ' ' + sCell)
    iResult = 0
    raise
Antworten