Second version of the script... if anyone needs that for another tool.
Code:
import csv
import base64
#Converts the catalog (as csv) exported from Calibre for import with covers into PortoDB (android)
#assume cover paths are in the first column of the csv
#NEED MANUALLY ADDING a COMMA at the beginning of the result file...
ipath = 'C:\Users\Fra\Documents\_permanent\Papier20150903last.csv'
wpath = 'C:\Users\Fra\Documents\_permanent\Papier20150903Coverlast5.csv'
#requires no space in the path... pity it would be simpler with the normal Calibre\ressource\image path
defaultcover = "C:\PortableApps\default_cover.png"
with open(ipath, 'rb') as file1, open(wpath, 'wb') as file2:
reader = csv.reader(file1, delimiter=',' , quotechar='"', doublequote = True)
#No quotes around the headers
#write without looking for path
headers = next(reader, None) # returns the headers or `None` if the input is empty
if headers:
writer = csv.writer(file2, delimiter=',', quoting=csv.QUOTE_NONE)
#tried to add the comma but it seems to corrup the file
#headers.insert(0, "")
writer.writerow(headers)
#For all other lines, put double quotes around each cell
writer = csv.writer(file2, delimiter=',', quoting=csv.QUOTE_ALL)
#count the book to give them an id
n = 1
for i, row in enumerate(reader):
if row[0] == "": row[0] = defaultcover
#else:
with open(row[0], "rb") as image_file:
s = base64.b64encode(image_file.read())
#trying to add line feeds at 76 bytes... not working yet
#'\n'.join(s[pos:pos+76] for pos in xrange(0, len(s), 76))
row[0] = s
#insert a book id
row.insert(0, n)
writer.writerow(row)
n += 1
Cheers
François