Suppose I have a text-file named barcodes.txt that looks like this:
some-13-digit-number
some-other-13-digit-number
I want to take each line from this file and output barcodes in a .svg-file. This I've done already using Barcode:
import barcode as bc
import os
path = os.path.dirname(os.path.abspath(__file__))
read_file = 'barcode.txt'
lines = (line.rstrip('\n') for line in open(path+read_file))
i = 0
for line in lines:
image = bc.get_barcode_class('ean13')
image_bar = image(u'{}'.format(int(line))) # This is byte-data as I understand it.
barcode_file = open(path+'ean'+str(i)+'.svg', 'wb')
image_bar.write(barcode_file)
i += 1
This works just fine. Never mind all the missing checks of input, I stripped a lot off to make this short and easily read.
The problem: I would like to write every barcode to the same file, instead of iterating through barcodes and write each in their own file. I've tried the obvious of just writing to the same file-name, but that gives me a header-issue. The resulting svg-file looks something like this on inspection:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg
PUBLIC '-//W3C//DTD SVG 1.1//EN'
'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>
<svg height="23.000mm" version="1.1" width="44.000mm" xmlns="http://www.w3.org/2000/svg">
<!--Autogenerated with python-barcode 0.9.0-->
<g id="barcode_group">
<rect height="100%" style="fill:white" width="100%"/>
<rect height="15.000mm" style="fill:black;" width="0.330mm" x="6.500mm" y="1.000mm"/>
<rect height="15.000mm" style="fill:white;" width="0.330mm" x="6.830mm" y="1.000mm"/>
<rect height="15.000mm" style="fill:black;" width="0.330mm" x="7.160mm" y="1.000mm"/>
...
And if I write to the same file, this header is repeated for each written barcode, which result in an error, and furthermore all the barcodes (rect-elements) overlap. So I'm looking for a different solution. Any suggestions? I'm open to use a different library if one fits the requirement that I can fetch all the numbers to generate barcodes from, from a single .txt-file.
For me the solution was to first generate one file for each line:
for line in lines:
filename = "ean"+str(i)+".png"
barcodefilepath = barcodeimagefolder/filename
if len(line) == 12 or len(line) == 13: #Let's assume these are ean13-barcodes.
image = bc.get_barcode_class('ean13')
image_bar = image(u'{}'.format(int(line)), writer=ImageWriter(), text_distance=1)
barcode_file = open(barcodefilepath, "wb")
image_bar.write(barcode_file)
barcode_file.close()
i += 1
Then I added all these files in a list:
files_and_dirs = Path(barcodeimagefolder).glob('**/*')
images = [x for x in files_and_dirs if x.is_file() and x.suffix == '.png']
And linked to them in a Pandas Dataframe:
imagedata = list()
for image in images:
imagedata.append("<img src='barcodeimages/{0}' width='200'>".format(image.name))
d = {'Barcodes': imagedata}
df = pd.DataFrame(data=d)
Which I then could save as a HTML, or PDF through wkhtmltopdf. Lastly I deleted all the temporary files:
files = glob.glob(str(barcodeimagefolder.resolve())+'/ean*')
for ean_image in files:
os.remove(ean_image)