Hello everyone,

I need some help, I would like to merge two cells together within a row only (e.g) in a CSV file using python.
So far, I have 4 columns in the file, but now I would like to merge two cells in one, but I don't have any clue how to do it.

Can someone help me please? Thank you

I used the code below to write in the file.

    print "write in file"
    fobj = open(rawPathOutput, "w")
    outputLine = "%s;%s;%s;%s " % ( "Section", 
                                         "Polygon" )
    fobj.write(outputLine + "\n")
    for row in range(iRows):
        for col in range(iCols):

            if inputWU!=0 :
                pos = p
                #position of the partition in the map
                pos     = "("+str(row+1)+","+str(col+1)+")"

            position = row*iCols + col

            outputLine = "%s;%s;%s;%s " % (pos,
                                  valueTable.getValue(position, 0), 
                                  valueTable.getValue(position, 1), 
                                  valueTable.getValue(position, 2))
            print outputLine
            fobj.write(outputLine + "\n")


Which cells do you you want to merge ? what is their content ? What should the merged cell contain ?

The cells that I want to merge are columns (c and d "in excel" ) together and (e and f), only in the first row.

The merged cells only contain some text "line" for the first merged cell and for the second one "polygon".

Thank you :)


I think this is not possible in csv. Csv does not contain any cell style information. I was able to produce an open-document spreadsheet using the development version of lpod-python (the function to merge cells was added 1 month ago). Here is the code

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals, print_function, division
import os
from lpod.document import odf_new_document
from lpod.table import odf_create_table
from lpod.style import odf_create_style

if __name__=="__main__":

    data = [
        ["Section\n", "Total nb of Points\n", "Line\n", None, "Polygon\n", None],
        [None, "Value", "Total length", "Value", "Total length", "Value"],
        [None, 15, 1256.5, 20, 125.3, 50],

    cellstylenum = odf_create_style('table-cell', name='cellstylenum')
    cellstylenum.set_properties(area='paragraph', align='start')
    cellcenterred = odf_create_style('table-cell', name='cellcenterred')
    cellcenterred.set_properties(area = 'text', color="#FF0000")
    cellcenterred.set_properties(area='paragraph', align='center')
    celltopred = odf_create_style('table-cell', name='celltopred')
    celltopred.set_properties(area = 'text', color="#FF0000")
    celltopred.set_properties(area='paragraph', align='center')    

    col3cm = odf_create_style('table-column', width='3cm')
    col4cm = odf_create_style('table-column', width='4cm')
    row1cm = odf_create_style('table-row', height='1cm')

    document = odf_new_document('spreadsheet')
    document.insert_style(col3cm, automatic=True)
    document.insert_style(col4cm, automatic=True)
    document.insert_style(row1cm, automatic=True)
    document.insert_style(cellstylenum, automatic=True)
    document.insert_style(cellcenterred, automatic=True)
    document.insert_style(celltopred, automatic=True)

    body = document.get_body()
    table = odf_create_table(u"First Table", width = len(data[0]), height = len(data))

    for r in range(len(data)):
        for c in range(len(data[r])):
            if data[r][c] is None:
            z = table.get_cell((c, r))
            if r == 0 and c >= 2:
            elif r == 1 and c >= 2:
            elif r >= 2:
            table.set_cell((c, r), z)

    for column in table.get_columns():
        column.set_style(col4cm if column.x == 1 else col3cm)
        table.set_column(column.x, column)
    row = table.get_row(0)
    table.set_row(row.y, row)

    #print("table size:", table.get_size())


    test_output_dir = 'test_output'
    if not os.path.exists(test_output_dir):
    output = os.path.join(test_output_dir, "spreadsheet2.ods")
    document.save(target = output, pretty=True)

That's what I thought as well... Thank you anyway, I will have look at it.