1

my program creates a bunch of word files :they contain informations about students such as Name address Phone number ... and a barcode . When I insert my QRCode as a picture , it is appended at the end of the file . I want it to be inserted at the very right of the text "Barcode" . I looked for a solution in vain . I inserted a picture of the word explain . Sorry I've hidden the informations because it is confidential. Here is my Code :

import uuid 
import pandas as pd
import pyqrcode 
from docx import Document
from docx.shared import Inches


def generateBarCode(length):
    return str(uuid.uuid4()).replace('-','')[0:length]


df=pd.DataFrame(pd.read_excel('Komplette- 
GastAccountFHZugangsdatenFertig.xlsx'))
array=[]
for i in range(len(df)):
    qr=pyqrcode.create(generateBarCode(8))
    array.append(qr)


df.insert(8,'Barcode',array)
x=['.png']

with pd.ExcelWriter('Komplette-GastAccountFHZugangsdatenFertig.xlsx') as 
writer :
   df.to_excel(writer,sheet_name='Sheet1')

for i in range(len(df)):
    document=Document()
    document.add_heading('Informationen')
    document.add_paragraph('Name : ' + df['Name'][i])
    document.add_paragraph('Vorname : ' + df['Vorname '][i])
    document.add_paragraph('Geschlecht : ' + df['Geschlecht'][i])
    document.add_paragraph('Adresse(in Marokko) : ' + df['Adresse (in 
Marokko)'][i])
    document.add_paragraph('Telefonnummer : ' + str(df['Telefonnummer'][i]))
    document.add_paragraph('E-Mailadresse : ' + str(df['E-Mailadresse'][i]))
    document.add_paragraph('Studiengang : ' + df['Studiengang'][i] )
    document.add_paragraph('Semester : ' + str(df['Semester'][i]))
    document.add_paragraph('Barcode : ')
    qr=df['Barcode'][i]

    qr.png(df['Name'][i]+df['Vorname '][i]+x[0])
    document.add_picture(df['Name'][i]+df['Vorname '] 
[i]+'.png',width=Inches(2.0))

    document.save(df['Name'][i]+' '+ df['Vorname '][i]+'.docx')

Screenshot

enter image description here

Thanks

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Odess4
  • 420
  • 5
  • 17

2 Answers2

3

Looking at the implementation of Document.add_picture at the docs of python-docx here gives

def add_picture(self, image_path_or_stream, width=None, height=None):
    run = self.add_paragraph().add_run()
    return run.add_picture(image_path_or_stream, width, height)

Using this information, you can use

document.add_paragraph('Barcode : ')
        .add_run()
        .add_picture(df['Name'][i]+df['Vorname '] [i]+'.png',width=Inches(2.0))

to add the picture to the same paragraph. I haven't tested it, but I hope this does the trick.

BurningKarl
  • 1,176
  • 9
  • 12
1

insert an Image in a specific position in a word document with python

Using docx module of Python we can add image at specific position in document

Please refer to my post linked here for the same https://stackoverflow.com/a/70099088/17385292

dataninsight
  • 1,069
  • 6
  • 13