发布代码以更新excel的问题

时间:2018-10-01 14:20:52

标签: excel python-3.x xlrd xlwt xlutils

我试图读取excel(.xlsx)文件并向现有工作簿中添加新列。我检查了有关此问题的许多问题,但找不到实际的解决方法。以下是我完整的代码,我试图读取和查找最大列数,然后向其中添加5个自定义列。在这样做的同时,我面临以下问题

代码:

import xlrd
from xlwt import Workbook
from xlrd import open_workbook
from xlutils.copy import copy
import os, sys, logging,re,argparse
from datetime import date
from bokeh.io import show, output_file
import bokeh.models 

def process_compare_and_create(loc):
    path=loc
    closeNoteIndex=0
    maxColumnNo=0
    cols=[]
    inputXlsxLocation=loc
    closeNoteIndex,maxColumnNo,cols=read_and_find_col_details(path,closeNoteIndex,maxColumnNo)
    update_new_column_to_existing_worksheet(path,cols)

def update_new_column_to_existing_worksheet (path,cols):
    rb = open_workbook(path,on_demand = True)
    wb = copy(rb)
    s = wb.get_sheet(0)
    newCol=['RCA','Owner','Mass failure','SLA Breach','Remarks','Workaround']
    for i in range(len(newCol)):
        if newCol[i] not in cols:
            s.write(0,maxColumnNo+i,newCol[i])
            print(newCol[i]) 
    wb.save(path)
    rb.release_resources()
    del wb,rb

def read_and_find_col_details(loc,closeNoteIndex,maxColumnNo):
    varCloseNotes='Close Notes'
    cols=[]
    wb = xlrd.open_workbook(loc,on_demand = True) 
    sheet = wb.sheet_by_index(0) 
    sheet.cell_value(0, 0) 
    for i in range(sheet.ncols): 
        print (i, sheet.cell_value(0, i))
        cols.append(sheet.cell_value(0, i))

        if sheet.cell_value(0,i).lower()==varCloseNotes.lower():
            closeNoteIndex=i
        maxColumnNo=i
    print(cols)
    wb.release_resources()
    del wb
    print (closeNoteIndex,maxColumnNo,cols)
    return closeNoteIndex,maxColumnNo,cols

if __name__=="__main__":
    print("start of batch operations")
    loc=r'C:\Users\satya\Desktop\dump.xlsx' 
    process_compare_and_create(loc)

我遇到了以下错误。

错误:     在“ C:\ Users \ satya \ Desktop \ dump.xlsx”中找到了该定义,因为它是已编译的扩展名,所以无法打开该定义”

输出异常:

start of batch operation
0 Number
1 Created
2 Severity
3 Assigned to
4 Short description
5 Assignment group
6 Close notes
['Number', 'Created', 'Severity', 'Assigned to', 'Short description',
'Assignment group', 'Close notes', 'RCA', 'Owner', 'Mass failure', 'SLA 
Breach', 'Remarks', 'Workaround']
13 25 ['Number', 'Created', 'Severity', 'Assigned to', 'Short description', 
'Assignment group', 'Close notes', 'RCA', 'Owner', 'Mass failure', 'SLA 
Breach', 'Remarks', 'Workaround']
Traceback (most recent call last):
 File "C:\Users\satya\workspace\archival script\SatRepo.py", line 78, in <module>
 process_compare_and_create(loc)
 File "C:\Users\satya\workspace\archival script\SatRepo.py", line 28, in process_compare_and_create
update_new_column_to_existing_worksheet(path,cols)
File "C:\Users\satya\workspace\archival script\SatRepo.py", line 40, in update_new_column_to_existing_worksheet
wb.save(path)
File "C:\Users\satya\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\Workbook.py", line 710, in save
doc.save(filename_or_stream, self.get_biff_data())
File "C:\Users\satya\AppData\Local\Programs\Python\Python36\lib\site-packages\xlwt\CompoundDoc.py", line 263, in save
f = open(file_name_or_filelike_obj, 'w+b')
OSError: [Errno 22] Invalid argument: C:\\Users\\satya\\Desktop\\dump.xlsx

任何有关此的帮助都将非常有用。

0 个答案:

没有答案