无法写一个xslx文件,它说有一个属性错误

时间:2018-06-05 06:04:46

标签: python xlsx xlsxwriter

[嗨,我写了一些类似下面的代码,但我的最后一部分有问题。我已经使用" pip install xlsxwriter"多次安装xlsxwrtier,所以我有最新版本。但我仍然在努力与#34; Workbook"事情。我怎样才能解决这个问题?请问有什么建议吗?]

 import xlrd
 import xlsxwriter

 def writeToExcel(wantToSave,name):
     numberOfRows = len(wantToSave)
     workbook = xlsxwriter.WorkBook(name +' .xlsx')
     worksheet = workbook.add_worksheet()
     for i in range(numberOfRows):
         worksheet.write(i,0, str(wantToSave[i]))
     workbook.close()

 def excelReader(datafile, sheet_num):
     workbook = xlrd.open_workbook(datafile)
     sheet = workbook.sheet_by_index(sheet_num)
     data = [[sheet.cell_value(r,col) for col in range(sheet.ncols)] for r in range(sheet.nrows)]
     return data

data = excelReader("list1.xlsx", 0)
data.remove(data[0])
data

sentences = []
for i in range(len(data)):
  context_item = '[["'
  list_num = 'list '+str(int(data[i][0]))
  Item_No = str(int(data[i][1]))


  context_item += list_num+'", '+Item_No+'],'+' "DashedSentence", {s:["'
  sentence = data[i][5]
   if sentence[-1] == " ":
       sentence = sentence[:-1]
  context_item += sentence+'"]},'


  target_item = ' "DashedSentence", {s:["'
  sentence = data[i][6]

  if sentence[-1] == " ":
      sentence = sentence[:-1]
  target_item += sentence+'"]}, '+'"Question", {q:"'
  sentence = data[i][7]
  if sentence[-1] == " ":
    sentence = sentence[:-1]

  target_item += sentence+'"}],' 

  sentences.append(context_item+target_item)

  writeToExcel(sentences, 'ibex_list1')

  AttributeError                            Traceback (most recent call last)
 <ipython-input-15-3fda3a6a33ea> in <module>()
 ----> 1 writeToExcel(sentences, 'ibex_list1')

 <ipython-input-7-0d092c570855> in writeToExcel(wantToSave, name)
  1 def writeToExcel(wantToSave,name):
  2     numberOfRows = len(wantToSave)
  ----> 3     workbook = xlsxwriter.WorkBook(name +' .xlsx')
  4     worksheet = workbook.add_worksheet()
  5     for i in range(numberOfRows):

  AttributeError: module 'xlsxwriter' has no attribute 'WorkBook'

1 个答案:

答案 0 :(得分:1)

  

AttributeError:module'xlsxwriter'没有属性'WorkBook'

构造函数名称为/[\x30-\x39\x61-\x7A]/g而不是Workbook