python-docx 将图片放入 table 时出现问题
Problem with python-docx putting pictures in a table
我正在使用 python-docx 创建一个新文档,然后添加一个 table(行数=1,列数=5)。然后我为五个单元格中的每一个添加一张图片。我的代码可以正常工作,但我从 docx 中看到的内容与我手动使用 Word 时看到的内容不同。
具体来说,如果我设置“显示格式标记”然后查看 docx 生成的内容,每个单元格的开头总是有一个硬 return(由 add_paragraph方法。)当我手动使用Word时,没有hard return.
硬 return 的结果是每张图片都比我想要的位置低一行。如果我使用 Word,图片就在我期望的位置。
同样奇怪的是,在docx文档上我可以手动进去,在hard旁边单击一下return,按一次下光标键,再按一次Backspace键,然后按hard return被删除,图片移动到单元格顶部。
所以我的问题是,有谁知道在执行 add_paragraph 方法时无需硬 return 就可以在 table 单元格中获取图片的方法吗?
如有任何帮助,我们将不胜感激。
def paragraph_format_run(cell):
paragraph = cell.add_paragraph()
format = paragraph.paragraph_format
run = paragraph.add_run()
format.space_before = Pt(0)
format.space_after = Pt(0)
format.line_spacing = 1.0
format.alignment = WD_ALIGN_PARAGRAPH.CENTER
return paragraph, format, run
def main():
document = Document()
sections = document.sections
section = sections[0]
section.top_margin = Inches(1.0)
section.bottom_margin = Inches(1.0)
section.left_margin = Inches(0.75)
section.right_margin = Inches(0.75)
table = document.add_table(rows=1, cols=5)
table.allow_autofit = False
cells = table.rows[0].cells
for i in range(5):
pic_path = f"Table_Images\pic_{i}.jpg"
cell = cells[i]
cell.vertical_alignment = WD_ALIGN_VERTICAL.TOP
cell_p, cell_f, cell_r = paragraph_format_run(cell)
cell_r.add_picture(pic_path, width=Inches(1.25))
doc_path = "TableTest_1.docx"
document.save(doc_path)
新创建的 table 中的每个空白单元格都包含一个空段落。这只是关于 Word 格式的事情之一。我想当您使用 Word 应用程序时,它提供了放置插入标记(闪烁的垂直光标)的位置。一个完全空的单元格将没有地方可以“点击”进入。
这要求任何向单元格添加内容的代码都必须区别对待第一段。简而言之,您以 cell.paragraphs[0]
访问第一段,并且仅使用 cell.add_paragraph()
.
创建第二段和后面的段落
所以在这种特殊情况下,paragraph_format_run()
函数会像这样改变:
def paragraph_format_run(cell):
paragraph = cell.paragraphs[0]
...
这假设很多,就像它只在 cell
为空时有效,但鉴于您现在对单元格段落的了解,如果以后决定您可以调整它以将多个图像添加到一个单元格中需要那个。
我正在使用 python-docx 创建一个新文档,然后添加一个 table(行数=1,列数=5)。然后我为五个单元格中的每一个添加一张图片。我的代码可以正常工作,但我从 docx 中看到的内容与我手动使用 Word 时看到的内容不同。
具体来说,如果我设置“显示格式标记”然后查看 docx 生成的内容,每个单元格的开头总是有一个硬 return(由 add_paragraph方法。)当我手动使用Word时,没有hard return.
硬 return 的结果是每张图片都比我想要的位置低一行。如果我使用 Word,图片就在我期望的位置。
同样奇怪的是,在docx文档上我可以手动进去,在hard旁边单击一下return,按一次下光标键,再按一次Backspace键,然后按hard return被删除,图片移动到单元格顶部。
所以我的问题是,有谁知道在执行 add_paragraph 方法时无需硬 return 就可以在 table 单元格中获取图片的方法吗?
如有任何帮助,我们将不胜感激。
def paragraph_format_run(cell):
paragraph = cell.add_paragraph()
format = paragraph.paragraph_format
run = paragraph.add_run()
format.space_before = Pt(0)
format.space_after = Pt(0)
format.line_spacing = 1.0
format.alignment = WD_ALIGN_PARAGRAPH.CENTER
return paragraph, format, run
def main():
document = Document()
sections = document.sections
section = sections[0]
section.top_margin = Inches(1.0)
section.bottom_margin = Inches(1.0)
section.left_margin = Inches(0.75)
section.right_margin = Inches(0.75)
table = document.add_table(rows=1, cols=5)
table.allow_autofit = False
cells = table.rows[0].cells
for i in range(5):
pic_path = f"Table_Images\pic_{i}.jpg"
cell = cells[i]
cell.vertical_alignment = WD_ALIGN_VERTICAL.TOP
cell_p, cell_f, cell_r = paragraph_format_run(cell)
cell_r.add_picture(pic_path, width=Inches(1.25))
doc_path = "TableTest_1.docx"
document.save(doc_path)
新创建的 table 中的每个空白单元格都包含一个空段落。这只是关于 Word 格式的事情之一。我想当您使用 Word 应用程序时,它提供了放置插入标记(闪烁的垂直光标)的位置。一个完全空的单元格将没有地方可以“点击”进入。
这要求任何向单元格添加内容的代码都必须区别对待第一段。简而言之,您以 cell.paragraphs[0]
访问第一段,并且仅使用 cell.add_paragraph()
.
所以在这种特殊情况下,paragraph_format_run()
函数会像这样改变:
def paragraph_format_run(cell):
paragraph = cell.paragraphs[0]
...
这假设很多,就像它只在 cell
为空时有效,但鉴于您现在对单元格段落的了解,如果以后决定您可以调整它以将多个图像添加到一个单元格中需要那个。