【问题标题】:Add image caption using python-docx使用 python-docx 添加图片标题
【发布时间】:2020-11-13 21:25:43
【问题描述】:

我正在使用 python-docx 生成包含带有图像的表格的 Microsoft Word 文档。以下代码块显示了将图像添加到表中的 for 循环:

        row_num = 2
        img_cnt = 0
        for i, var in enumerate(img_list_obj):
            img = "img/NO-" + str(technical_object) + "/" + img_list_obj[i]
            img_cnt = img_cnt + 1
            row = table.rows[row_num].cells

            if (img_cnt % 2) != 0:
                paragraph = row[0].paragraphs[0]
                paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
                table.cell(row_num, 0).vertical_alignment = WD_ALIGN_VERTICAL.CENTER
                run = paragraph.add_run()
                run.add_picture(img, width=Cm(7.50), height=Cm(5.50))
            else:
                paragraph = row[1].paragraphs[0]
                paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
                table.cell(row_num, 1).vertical_alignment = WD_ALIGN_VERTICAL.CENTER
                run = paragraph.add_run()
                run.add_picture(img, width=Cm(7.50), height=Cm(5.50))
                if i + 1 != len(img_list_obj):
                    table.add_row()
                    row = table.rows[row_num + 1].cells
                    row_num = row_num + 1

有没有办法给图片添加标题?我对 Python 和 python-docx 比较陌生,但我找不到有关此主题的任何文档。

非常感谢! 约书亚

【问题讨论】:

    标签: python image python-docx


    【解决方案1】:

    python-docx 似乎不支持图片说明。当您调用 add_picture 时,将返回 InlineShape according to the documentationdocumentation for InlineShape 没有提及任何关于字幕的内容。 事实上,唯一提到的字幕是in the context of text styles。 此外,还有关于此限制的an open issue in the python-docx repository

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 2020-08-18
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多