-1

I have this piece of code that creates for each picture in a folder, a table and a picture in word file. I am using docx library with python in order to do so. The code works but it adds everything at the end of my file. What I am trying to figure out is how to place it in a WORD document ?

 p1 = document.add_paragraph("Here are the following pictures i took")

i=1
j = 0
for filename in os.listdir('Folder/Pictures'):            
    f = os.path.join('Folder/Pictures',filename)           
    if os.path.isfile(f):

        document.add_heading('Picture {}'.format(i), 4)
        i = i +1
        table = document.add_table(rows=1, cols=6)
        table.style = 'Table Grid'

        row = table.rows[0].cells
        row[0].text = 'Name'
        row[1].text = 'Date'
        row[2].text = 'Place'
       
        try:
            pic_name= pic_data[j][0]
            pic_date= pic_data[j][1]      
            pic_place= pic_data[j][2]
            
            
            rowCells = table.add_row().cells
            rowCells[0].text = pic_name
            rowCells[1].text = pic_date
            rowCells[2].text = pic_place

        except:
            raise RuntimeError("Tableau non trouvé")
     
        document.add_picture(f,width=Inches(8), height=Inches(5))
    j = j +1 

I found how to use position for paragraphs : document.paragraphs[139].text = document.paragraphs[139].text + 'test'

And also find this link but it works for paragraphs and not my whole piece of code : python-docx-insert-a-paragraph-after

So, How to write at a specific position in a WORD file ?

clemdcz
  • 99
  • 7

1 Answers1

0

Click where you want to add the index. On the References tab, in the Index group, click Insert Index. In the Index dialog box, you can choose the format for text entries, page numbers, tabs, and leader characters. You can change the overall look of the index by choosing from the Formats dropdown menu. ... Click OK.