我想使用xlsx writer将数据动态写入excel文件。我从mysql db获取数据,结果如下所示。

data  = (
    ('Shankar', '100', '100', '200'),
    ('Kumar', '50', '80', '130'),
    ('Shiva', '20', '20', '40'),
    ('Sekar', '20', '20', '40')
)


我想在python中使用xlsxwriter将上述数据写入excel文件。预期结果应如下所示

  Shankar 100  100  200
  Kumar    50   80  130
  Shiva    20   20   40
  Sekar    20   20   40


请任何人帮助我在python中使用xlsxwriter编写代码

最佳答案

试试看:

import xlsxwriter

data  = (('Shankar','100','100','200'),('Kumar','50','80','130'),('Shiva','20','20','40'),('Sekar','20','20','40'))

# Open the workbook and add a worksheet
workbook = xlsxwriter.Workbook('output.xlsx')
worksheet = workbook.add_worksheet()

# iterate through your tuple of tuples, and write out each cell
for row, x in enumerate(data):
    for col, y in enumerate(x):
        worksheet.write(row, col, y)

# Make sure to close the workbook
workbook.close()


给出以下output.xlsx:

python - 如何使用XLSX Writer动态写入Excel-LMLPHP

10-08 13:24