When trying to load csv file data into Scribus it only takes the data of the last row.
I'm using ScribusGenerator.py by Ekkehard Will and my best guess is that the cause of the problem is somewhere in this part of the code:

def replaceVariablesWithCsvData(self, headerRow, row, lines): # lines as list of strings
result = ''
for line in lines:
i = 0
for cell in row:
tmp = ('%VAR_' + headerRow[i] + '%')
line = line.replace(tmp, cell) # string.replace(old, new)
i = i + 1
result = result + line
return result

def getCsvData(self, csvfile):

Read CSV file and return 2-dimensional list containing the data

reader = csv.reader(file(csvfile))
result = []
for row in reader:
rowlist = []
for col in row:
rowlist.append(col)
result.append(rowlist)
return result

Your indention is incorrect use code button to insert code or data files.

Edited 4 Years Ago by pyTony

def replaceVariablesWithCsvData(self, headerRow, row, lines): # lines as list of strings
        result = ''
        for line in lines:
            i = 0
            for cell in row:
                tmp = ('%VAR_' + headerRow[i] + '%')
                line = line.replace(tmp, cell) # string.replace(old, new)
                i = i + 1
            result = result + line
        return result

def getCsvData(self, csvfile):
        # Read CSV file and return  2-dimensional list containing the data
        reader = csv.reader(file(csvfile))
        result = []
        for row in reader:
            rowlist = []
            for col in row:
                rowlist.append(col)
            result.append(rowlist)
        return result

def replaceVariablesWithCsvData(self, headerRow, row, lines): # lines as list of strings

This function does not create a list. It creates one long string. For more precise help come up with a simple example and some test data so we can see what happens compared to what should happen.

A sample csv file looks like this:
"test","pricea","qtya","priceb","qtyb","pricec","qtyc"
"perrytest","11,22",5,"9,55",10,"8,88",50
"testrow2",12,5,10,10,9,50
"testrow3",10,5,9,10,"8,75",50

My Scribus template has variables %VAR_test%, %VAR_pricea%, %VAR_qtya%, %VAR_priceb%, %VAR_qtyb%,
%VAR_pricec%, %VAR_qtyc%
The output I get from the generator is only the last line (testrow3) whereas I want to see all the data from line 2, 3 and 4.

You have to test functions instead of guessing where an error might be. To test the read function print the results, or the first 10 or so records if the file is big.

import csv

def get_csv_data(csvfile):
    """ A simpler way
    """
    with open(csvfile) as fp:
        reader = csv.reader(fp)
        reader_as_list=list(reader)
    return reader_as_list

def get_csv_data_2(csvfile):
    # Read CSV file and return  2-dimensional list containing the data
    reader = csv.reader(file(csvfile))
    result = []
    for row in reader:
        rowlist = []
        for col in row:
            rowlist.append(col)
        result.append(rowlist)
    return result

##===================================================================
test_data = """test,pricea,qtya,priceb,qtyb,pricec,qtyc
perrytest,11,22,5,9,55,10,8,88,50
testrow2,12,5,10,10,9,50
testrow3,10,5,9,10,8,75,50"""

## write a test csv file
fname = './test_it.csv'
with open(fname, 'w') as csvfile:
    for rec in test_data.split("\n"):
        csvfile.write("%s\n" % (rec))

return_list=get_csv_data(fname)
print "return list", return_list
print "\ncsv_2 data---------------------------------------"
return_list=get_csv_data_2(fname)
print return_list

To test the replace variables function (we don't know what "row" is or what the replace is supposed to do, so it's up to you), print the length of the list passed to the function and see if it is reasonable, and then print the first 10 records before and after the replace statement.

Edited 4 Years Ago by woooee

The replace variables function is supposed to replace the variables used in the Scribus template with cell values from the csv table, but the ScribusGenerator script only takes the cell values of the last row. Somehow the iteration is wrong.
With your version I get the same data twice.

This article has been dead for over six months. Start a new discussion instead.