PictureElement (arcpy.mapping)

摘要

用于访问图片属性,从而在页面布局中重新定位图片并获取和设置其数据源。

讨论

PictureElement 对象代表已插入到页面布局中的栅格或影像。ListLayoutElements 函数将返回页面布局元素对象的 Python 列表。随后遍历列表中的每个项目,或指定一个索引号以引用具体的页面元素对象。要返回一个只包含 PictureElements 的列表,应对 element_type 参数使用 PICTURE_ELEMENT 常量。可使用通配符根据元素名称优化搜索过程。

建议您为每个页面布局元素赋予唯一的名称,以便在 Python 中进行区分。X 和 Y 值以元素的锚点位置为基础,可通过 ArcMap 中元素属性对话框的大小和位置选项卡进行设置。

PictureElement 对象的 sourceImage 属性用于读取或修改图片源位置。如果您计划用不同大小和纵横比的图片替换原有图片,则应创建设置了图片高度和宽度的地图文档,其中的高度和宽度代表所有图片在页面布局中占据的范围。使用 sourceImage 属性替换某个图片且新图片具有不同的横纵比时,将使用最长边调整新图片,使其适应原始图片区域。替换的图片绝不会超过原始定义的尺寸。您最好还设置锚点位置,以便所有新图片都相对于该位置进行调整。如果不希望图片偏斜,请确保选中保留纵横比选项。

属性

属性说明数据类型
elementHeight
(读写)

The height of the element in page units. The units assigned or reported are in page units.

Double
elementPositionX
(读写)

The x location of the data frame element's anchor position. The units assigned or reported are in page units.

Double
elementPositionY
(读写)

The y location of the data frame element's anchor position. The units assigned or reported are in page units.

Double
elementWidth
(读写)

The width of the element in page units. The units assigned or reported are in page units.

Double
name
(读写)

The name of the element.

String
sourceImage
(读写)

A text string that represents the path to the image data source.

String
type
(只读)

Returns the element type for any given page layout element.

  • DATAFRAME_ELEMENTData frame element
  • GRAPHIC_ELEMENTGraphic element
  • LEGEND_ELEMENTLegend element
  • MAPSURROUND_ELEMENTMap surround element
  • PICTURE_ELEMENTPicture element
  • TEXT_ELEMENTText element
String

代码实例

PictureElement 示例 1

以下脚本将按名称查找影像,并将其数据源设置为新路径。

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
for elm in arcpy.mapping.ListLayoutElements(mxd, "PICTURE_ELEMENT"):
    if elm.name == "Photo":
        elm.sourceImage = r"C:\Project\Data\NewPhoto.bmp"
mxd.save()
del mxd
PictureElement 示例 2

以下脚本显示在启用了“数据驱动页面”的地图文档中,各页面上的不同图片如何进行切换。每个页面上有不同的图片。这些图片命名为 Photo1.png、Photo2.png、Photo3.png 等,以匹配相应的页码。

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
pict = arcpy.mapping.ListLayoutElements(mxd, "PICTURE_ELEMENT", "photo")[0]
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    pict.sourceImage = r"C:\Project\Data\Photo{0}.png".format(pageNum)
    print("Exporting page {0} of {1}"
          .format(mxd.dataDrivenPages.currentPageID,
                  mxd.dataDrivenPages.pageCount))
    arcpy.mapping.ExportToPDF(mxd, r"C:\Project\Page{0}.pdf".format(pageNum))
del mxd
5/10/2014