簡體   English   中英

如何在WORD文件中寫入特定的position

[英]How to write at a specific position in a WORD file

我有這段代碼可以為文件夾、表格和 word 文件中的圖片創建每張圖片。 為此,我正在使用帶有 python 的 docx 庫。 該代碼有效,但它在我的文件末尾添加了所有內容。 我想弄清楚的是如何將其放入WORD文檔中?

 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 

我找到了如何將 position 用於段落: document.paragraphs[139].text = document.paragraphs[139].text + 'test'

並且還找到了這個鏈接,但它適用於段落而不是我的整個代碼: python-docx-insert-a-paragraph-after

那么,如何在WORD文件中寫入特定的position呢?

單擊要添加索引的位置。 在引用選項卡上的索引組中,單擊插入索引。 在“索引”對話框中,您可以選擇文本條目、頁碼、制表符和前導字符的格式。 您可以通過從格式下拉菜單中進行選擇來更改索引的整體外觀。 ... 單擊確定。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM