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", 
                                         "Points", 
                                         "Lines", 
                                         "Polygon" )
    fobj.write(outputLine + "\n")
    for row in range(iRows):
        for col in range(iCols):


            if inputWU!=0 :
                pos = p
            else:
                #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")

    fobj.close()

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 :)

img2

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))
    body.append(table)

    for r in range(len(data)):
        for c in range(len(data[r])):
            if data[r][c] is None:
                continue
            z = table.get_cell((c, r))
            z.set_value(data[r][c])
            if r == 0 and c >= 2:
                z.set_style(celltopred)            
            elif r == 1 and c >= 2:
                z.set_style(cellcenterred)
            elif r >= 2:
                z.set_style(cellstylenum)
            table.set_cell((c, r), z)
    table.set_span((2,0,3,0))
    table.set_span((4,0,5,0))

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

    #print("table size:", table.get_size())
    table.rstrip(aggressive=True)

    print(table.to_csv())

    test_output_dir = 'test_output'
    if not os.path.exists(test_output_dir):
        os.mkdir(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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.