如何将带有图形标题的图像添加到Word文档

时间:2018-12-14 05:27:44

标签: python vba ms-word pywin32 caption

import win32com.client as win32
import os

#creating a word application object
wordApp = win32.gencache.EnsureDispatch('Word.Application') #create a word application object
wordApp.Visible = True # hide the word application
doc = wordApp.Documents.Add() # create a new application

#Formating the document
doc.PageSetup.RightMargin = 10
doc.PageSetup.LeftMargin = 10
doc.PageSetup.Orientation = win32.constants.wdOrientLandscape
# a4 paper size: 595x842
doc.PageSetup.PageWidth = 595
doc.PageSetup.PageHeight = 842


# Inserting Tables
my_dir="C:/Users/David/Documents/EGi/EGi Plots/FW_plots/Boxplots"
filenames = os.listdir(my_dir)
piccount=0
file_count = 0
for i in filenames:
    if i[len(i)-3: len(i)].upper() == 'JPG': # check whether the current object is a JPG file
        piccount = piccount + 1

print piccount, " images will be inserted"

total_column = 1
total_row = int(piccount/total_column)+2
rng = doc.Range(0,0)
rng.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphCenter
table = doc.Tables.Add(rng,total_row, total_column)
table.Borders.Enable = False
if total_column > 1:
    table.Columns.DistributeWidth()

#Collecting images in the same directory and inserting them into the document


piccount = 1

for index, filename in enumerate(filenames): # loop through all the files and folders for adding pictures

    if os.path.isfile(os.path.join(os.path.abspath(my_dir), filename)): # check whether the current object is a file or not
        if filename[len(filename)-3: len(filename)].upper() == 'JPG': # check whether the current object is a JPG file
            piccount = piccount + 1
            print filename, len(filename), filename[len(filename)-3: len(filename)].upper()


            cell_column = (piccount % total_column + 1) #calculating the position of each image to be put into the correct table cell
            cell_row = (piccount/total_column + 1)
            #print 'cell_column=%s,cell_row=%s' % (cell_column,cell_row)

            #we are formatting the style of each cell
            cell_range= table.Cell(cell_row, cell_column).Range
            cell_range.ParagraphFormat.LineSpacingRule = win32.constants.wdLineSpaceSingle
            cell_range.ParagraphFormat.SpaceBefore = 0
            cell_range.ParagraphFormat.SpaceAfter = 3

            #this is where we are going to insert the images
            current_pic=cell_range.InlineShapes.AddPicture(os.path.join(os.path.abspath(my_dir), filename))


            #Currently this puts a lable in a cell after the pic, I want to put a proper ms word figure caption below the image instead.
            table.Cell(cell_row, cell_column).Range.InsertAfter("\n"+"Appendix II Figure "+ str(piccount-1)+": "+filename[:len(filename)-4]+"\n"+"\n"+"\n")


        else: continue

此代码将所有图像保存在选定目录中,并将它们放在word doc的表中,然后在下面的单元格中放置文件名(从extn中删除)。我想要一个适当的图形标题(以便在插入其他图片时这些图形会更新),但是我尝试过的所有操作都失败了。

我只是无法正确使用VB命令,这是:

table.Cell(cell_row, cell_column).Range.InsertAfter(InsertCaption(Label="Figure", Title=": "+filename[:len(filename)-4]))

在文档末尾给了我一个图形标题列表,这并不是我真正想要的。我觉得我已经接近了,但我无法完全理解。谢谢!

1 个答案:

答案 0 :(得分:1)

要使用Word的内置字幕而非txtInput.layer.borderWidth = 1 ,请使用current_pic.InsertCaptioncurrent_Pic.Range.InsertCaption方法是InsertCaption对象的成员,而不是Range对象的成员。对我来说,这会自动在图片下方的标题中插入标题。但是,如果您想具体地“使用以下”,也可以使用InlineShape参数:

Position

注意:当我测试代码行(在VBA中)时,FWIW表示您在文档末尾提供了标题列表,但确实在与插入图片相同的单元格中看到了文本。 / em>