2018 年 4 月 4 日更新:我想通了。见下文...
我正在尝试以编程方式将数百个文本框添加到 Google 幻灯片演示文稿中。我想设置文本中心水平对齐和中间垂直对齐。任何人都可以用文本框的文本举一个这样做的例子。我几乎在请求文本的每个位置都尝试了建议的 API 请求:
'ContentAlignment': 'MIDDLE'
&
'alignment': 'CENTER'
我会将这些行放在下面的代码中的什么位置?
def add_text_box(ss, org, elemID, presID):
# Create a new square textbox, using the supplied element ID.
height = {
'magnitude': 50,
'unit': 'PT'
}
width = {
'magnitude': 200,
'unit': 'PT'
}
requests = []
requests.append(
{
'createShape': {
'objectId': elemID,
'shapeType': 'TEXT_BOX',
'elementProperties': {
'pageObjectId': org,
'size': {
'height': height,
'width': width
},
'transform': {
'scaleX': 1,
'scaleY': 1,
'translateX': 10,
'translateY': 10,
'unit': 'PT'
}
}
}
}
)
# Insert text into the box, using the supplied element ID.
requests.append(
{
'insertText': {
'objectId': elemID,
'insertionIndex': 0,
'text': 'Position\nName\nDate'
}
}
)
# Change text style based on position in text string
requests.append(
{
'updateTextStyle': {
'objectId': elemID,
'textRange': {
'type': 'FIXED_RANGE',
'startIndex': 0,
'endIndex': 8
},
'style': {
'fontFamily': 'Arial',
'fontSize': {
'magnitude': 10,
'unit': 'PT'
},
},
'fields': 'fontFamily,fontSize'
}
}
)
requests.append(
{
'updateTextStyle': {
'objectId': elemID,
'textRange': {
'type': 'FIXED_RANGE',
'startIndex': 9,
'endIndex': 13
},
'style': {
'fontFamily': 'Arial',
'bold': True,
'fontSize': {
'magnitude': 14,
'unit': 'PT'
},
},
'fields': 'fontFamily,bold,fontSize'
}
}
)
requests.append(
{
'updateTextStyle': {
'objectId': elemID,
'textRange': {
'type': 'FIXED_RANGE',
'startIndex': 14,
'endIndex': 18
},
'style': {
'fontFamily': 'Arial',
'fontSize': {
'magnitude': 8,
'unit': 'PT'
},
},
'fields': 'fontFamily,fontSize'
}
}
)
# Execute the request.
body = {
'requests': requests
}
response = ss.presentations().batchUpdate(presentationId=presID, body=body).execute()
create_shape_response = response.get('replies')[0].get('createShape')
print('Created textbox with ID: {0}'.format(create_shape_response.get('objectId')))
OR 另一个requests
字符串中的代码。
更新:
要使形状中的文本水平居中,请在原始代码中添加以下内容:
requests.append(
{
'updateParagraphStyle': {
"objectId": elemID,
"style": {
"alignment": "CENTER"
},
"fields": 'alignment',
}
}
)
要使形状中的文本垂直定位在 MIDDLE 中,以及绘制实心轮廓并填充形状,请在原始代码中添加以下内容:
requests.append(
{
"updateShapeProperties": {
"objectId": elemID,
"fields": "outline,shapeBackgroundFill,contentAlignment",
"shapeProperties": {
"shapeBackgroundFill": {
"solidFill": {
"alpha": 0.6,
"color": {
"themeColor": "ACCENT5"
}
}
},
"outline": {
"dashStyle": "SOLID",
"outlineFill": {
"solidFill": {
"alpha": 1,
"color": {
"themeColor": "ACCENT5"
}
}
},
"weight": {
"magnitude": 3,
"unit": "PT"
}
},
"contentAlignment": 'MIDDLE'
}
}
}
)