Using xlwt vs openpyxl

I need help with openpyxl in PYTHON. I have successfully used xlwt, but now I have several files (in MySQL Workbench) that contain more than 65,000 lines. I know that I can create a CSV file, but XLSX is the preferred output. I can create a book using openpyxl, but I was unable to place MySQL data in a table. The bulk of the program using xlwt is fairly simple (see below). I just can't figure out how to do the same using openpyxl. I have tried several different combinations and solutions. I just got stuck after "for x as a result:".

file_dest = "c:\home\test.xls"
result = dest.execute("select a, b, c, d from filea)
for x in result:
    rw = rw + 1
    sheet1 = book.add.sheet('Sheet 1')
    row1 = sheet1.row(rw)
    row1.write(1,x[0])
    row1.write(1,x[1])
    row1.write(1,x[2])
    row1.write(1,x[3])
book.save(file_dest)
+4
source share
2 answers

append():

.

: ,

import openpyxl

file_dest = "test.xlsx"

workbook = openpyxl.Workbook()
worksheet = workbook.get_active_sheet()

result = dest.execute("select a, b, c, d from filea")
for x in result:
    worksheet.append(list(x))

workbook.save(file_dest)
+2

:

wb = Workbook(encoding='utf-8')
ws = wb.worksheets[0]
row = 2
ws.title = "Report"
ws.cell('A1').value = "Value"
ws.cell('B1').value = "Note"
for item in results:
    ws.cell('A%d' % (row)).value = item[0]
    ws.cell('B%d' % (row)).value = item[1]
    row += 1

http://pythonhosted.org//openpyxl/

+1

Source: https://habr.com/ru/post/1531574/


All Articles