如何从函数结果中列出清单?

时间:2019-04-10 08:58:04

标签: python python-3.x python-2.7 list xlsxwriter

当我运行代码时,它可以很好地工作并生成一个名为exel.xlsx的excel文件,但是exel.xlsx中没有任何信息。

我认为我在列出清单时犯了一个错误,但找不到解决方法。

def randStr(length=7):
    characters = list('bcdghijkmnpqrtuvwxyz23456789')
    shuffle(characters)
    exel = ''.join(characters[:length])
    listb = [exel]
    listb.append(exel)
    workbook = xlsxwriter.Workbook('Exel.xlsx')
    worksheet = workbook.add_worksheet('randomise')
    chart = workbook.add_chart({'type': 'line'})
    expenses = (listb)
    row = 0
    col = 0
    workbook.close()
    return exel

2 个答案:

答案 0 :(得分:0)

import xlsxwriter

workbook = xlsxwriter.Workbook('chart_line.xlsx')
worksheet = workbook.add_worksheet()

# Add the worksheet data to be plotted.
data = [10, 40, 50, 20, 10, 50]
worksheet.write_column('A1', data)

# Create a new chart object.
chart = workbook.add_chart({'type': 'line'})

# Add a series to the chart.
chart.add_series({'values': '=Sheet1!$A$1:$A$6'})

# Insert the chart into the worksheet.
worksheet.insert_chart('C1', chart)

workbook.close()

这是add_chart的基本方法,然后将值添加到图表并插入图表。 但是您没有在图表中添加任何值。此外,您没有在表中的列/行中添加任何值。 我运行了您的代码,创建了名为“ randomise”的工作表,但是其中没有数据,因为您还没有添加任何内容

答案 1 :(得分:0)

解决了!

def randStr(length=7):

    characters = list('bcdghijkmnpqrtuvwxyz23456789')
    shuffle(characters)
    listc=''.join(characters[:length])

    return listc

listb=[]

if __name__ == '__main__':
    for i in range(20):
        value1=(randStr())
        listb.append(value1)
        workbook = xlsxwriter.Workbook('Exel.xlsx')
        worksheet = workbook.add_worksheet('randomise')
        worksheet.write_column(0,0,listb)
        chart = workbook.add_chart({'type': 'line'})
        expenses =(listb)
        row = 0
        col = 0
        workbook.close()
        img = makeImage(value1, width=512)
        with open('%d.png' % i, 'wb') as f:
            f.write(img)
        print (i)