提问人:techDesign 提问时间:10/29/2023 最后编辑:Ben A.techDesign 更新时间:10/29/2023 访问量:13
如何使用 Python 用 docx 文件中的图片完全填充单元格
How to completely fill a cell with a picture in a docx file using Python
问:
如何使用单元格中的整个空间,以便图片完全覆盖它?我正在尝试使用 docx 格式的 Python 执行此操作。我尝试了以下方法,但会导致错误:
chart_cell = row_charts[i]
image = Image.open(chart)
# Assuming some margin, you can adjust this value
margin = Pt(2)
# Calculate the available space for the image
available_width = chart_cell.width - margin * 2
available_height = 50 # Set an approximate value for the available height
# Resize the image to fit the available space
image.thumbnail((available_width, available_height))
# Calculate the positioning to center the image within the cell
left = (chart_cell.width - image.width) // 2
top = (available_height - image.height) // 2
chart_cell.paragraphs[0].clear()
chart_cell.paragraphs[0].add_run().add_picture(chart, left=Inches(left), top=Inches(top))
答: 暂无答案
评论