2017-06-13 51 views
0

我試圖讓我的輸出從我的代碼進入一個excel電子表格,我不斷收到此特定錯誤:Python的PDF刮板輸出到Excel

「異常:嘗試覆蓋細胞:SHEETNAME = U」表1'rowx = 1 colx = 0「

我如何設置我的代碼是我需要它以便找到項目並打印所需的結果,因爲我正在使用它來刮擦.PDF並且它會產生如果我想把它全部寫入一個文本文件,則需要確切的輸出。但我希望輸出爲excel,因此我可以指定數據的位置。看起來,因爲代碼的輸出循環遍歷結果,它還會嘗試將所有內容都連續放在第0行第0列中,並且我會不斷收到有關覆蓋數據的錯誤。

from xlwt import Workbook, Formula, easyxf 
    wb = Workbook() 
    sheet1 = wb.add_sheet('Sheet 1') 
    lines = open("register.txt", "r").readlines() 

    search_counters = { 
     "Per End": 0, 
    } 
    lines=[line for line in lines if line] # removes empty lines, if there are any 
    for i, line in enumerate(lines): 
     for search_key in search_counters.keys(): 
      if search_key in line: 
       # print the previous line if the current line contains "Per End" 
       search_counters[search_key] += 1 
       if search_key == "Per End": 
        print lines[i-1] 
        EE_Name = lines[i-1] 
    wb.save('payroll2.xls') 
+0

對不起,太模糊了。我不想輸出覆蓋。我需要它列出數據輸出到第一列的新行。 –

回答

0

XLWT不隱式地允許單元覆蓋。看到答案了類似的問題在這裏: Python XLWT attempt to overwrite cell workaround

基本上得到了覆蓋除你身邊需要你的表單生成線看起來像這樣:

sheet1 = wb.add_sheet('Sheet 1', cell_overwrite_ok=True) 

但這樣做會導致你的表1總是被覆蓋。那是你要的嗎?

+0

我不希望它覆蓋。我希望他們只列出下一行的數據,直到沒有更多數據要通過。 –