Python Forum

Full Version: XlsxWriter: How can I append new data into not blank cells?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
to understand better what I want to do, I'll show you a simple example. following a little script to create an excel file named "test.xlsx" via XlsxWriter module. in this document we have only a column called "NAMES" and the name "Vittorio Emanuele" in the A:2 cell:

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet("TEST")

cell_format_1 = workbook.add_format({'bold': True, 'valign': 'left'})
cell_format_2 = workbook.add_format({'valign': 'left'})

worksheet.write(0, 0, "NAMES", cell_format_1)
worksheet.write(1, 0, "Vittorio Emanuele", cell_format_2)

worksheet.set_column(0, 0, 16.71)

workbook.close()
following picture shows the script's result:

[Image: 5dff0e1238092764.jpg]

what about if I want to add a second name, for example "Alessandro Manzoni" in A2 cell like before, but without overrite the name "Vittorio Emanuele"? I would very like to have this reluslt: "Vittorio Emanuele\nAlessandro Manzoni". see the picture:

[Image: d376781238093964.jpg]

I need to find a command to do that, and use it in others programs that I wtote.
This works on my PC:
worksheet.write(1, 0, "Vittorio Emanuele\nAlessandro Manzoni")
worksheet.set_row(1,50) # row height
Or use openpyxl:
#!/usr/bin/python3
from openpyxl import Workbook
wb = Workbook()

ws = wb.active
ws['A1'] = 'Names'

ws['A2']  = 'Vittorio'
ws['A2']  = ws['A2'].value + '\nAlexxandro'

wb.save("test.xlsx")