如何使用XLSXWriter绘制形状

问题描述 投票:0回答:2

我想使用 XLSXWriter 在 Excel 文件中绘制一些简单的形状,例如“箭头、直线、矩形、椭圆形”,但我可以找到任何示例来实现。是否可以 ?如果没有的话,python 的哪个库可以做到这一点? 谢谢!

python xlsxwriter
2个回答
1
投票

可能吗?

不幸的是没有。除文本框外,XlsxWriter 不支持形状。


0
投票

不幸的是,XlsxWriter 和 OpenPyxl 不支持在 Excel 中绘制形状。

使用 XlWings 可能会起作用:

https://docs.xlwings.org/en/stable/api/shape.html#xlwings.Shape

对于 PowerPoint,您可以使用 python-pptx 包:

https://python-pptx.readthedocs.io/en/latest/api/shapes.html#pptx.shapes.shapetree.SlideShapes.add_shape

from pptx import Presentation
from pptx.util import Inches
from pptx.dml.color import RGBColor
from pptx.enum.shapes import MSO_SHAPE

# Create a presentation object
prs = Presentation()

# Add slide
slide_layout = prs.slide_layouts[5]  # Choosing a blank slide layout
slide = prs.slides.add_slide(slide_layout)

# Remove title placeholder
textbox = slide.shapes[0]
sp = textbox.element
sp.getparent().remove(sp)

# Add shape
size = 3
left = 0
top = 0
width = Inches(size)
height = Inches(size)
shape = slide.shapes.add_shape(
    MSO_SHAPE.HEXAGON, left, top, width, height
)

# Set the fill color 
fill = shape.fill
fill.solid()
fill.fore_color.rgb = RGBColor(100, 100, 100)

# Set the outline color
outline = shape.line
outline.fill.solid()
outline.fill.fore_color.rgb = RGBColor(0, 0, 0)

# Add text 
shape.text = 'Hello world!'

# Save the presentation
prs.save('z_output.pptx')

相关:

如何使用 Python 添加和修改 Excel 文件中的形状

© www.soinside.com 2019 - 2024. All rights reserved.