如何使用pythondocx向同一docx中的图像添加超链接?

2024-04-18 21:58:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python docx创建一个docx文件,其中包含一些数据、表格和图像,这些数据、表格和图像是从PDF转换而来的,最后使用add\u picture()附加。你知道吗

现在我有了一个表,其中每一行都包含一个页码,我想将该页码链接(超链接/交叉引用)到插入同一文档中的图像。有没有任何方法可以使用python docx来实现这一点。你知道吗

对于表,我使用的数据来自dataframe,它有4列date,page。否、名称和报告类型

创建python docx表如下:

t = document.add_table(df.shape[0] + 1, df.shape[1], style='Table Grid')


# add the header rows.

for j in range(df.shape[-1]):
    t.cell(0, j).text = df.columns[j]

# add the rest of the data frame

for i in range(df.shape[0]):
    for j in range(df.shape[-1]):
        t.cell(i + 1, j).text = str(df.values[i, j])
        if i % 2 == 0:
            shading_elm = \
                parse_xml(r'<w:shd {} w:fill="c5d9f1"/>'.format(nsdecls('w'
                          )))
            t.cell(i + 1, j)._tc.get_or_add_tcPr().append(shading_elm)
        else:
            pass



#inserting images as follows:-

document.add_picture('00001.tif', width=Inches(6), height=Inches(10))
document.add_picture('00002.tif', width=Inches(6), height=Inches(10))
document.add_picture('00003.tif', width=Inches(6), height=Inches(10))
document.save('demo.docx')

现在当我点击页码它应该带我到一个附加的图像列。你知道吗


Tags: the数据in图像adddfforcell