如何调整工作表上所有图像的大小?

时间:2018-09-25 16:50:45

标签: vba excel-vba

我在工作表上有几张图像。我想将它们的大小调整为相同的大小,但似乎无法正常工作。我以为会像下面的代码,但这实际上使所有内容都具有不同的大小。

import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(19680801)


plt.rcdefaults()
fig, ax = plt.subplots()

# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))

ax.barh(y_pos, performance, xerr=error, align='center',
        color='green', ecolor='black')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis()  # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?')

plt.show()

2 个答案:

答案 0 :(得分:5)

我认为您只是想念一件小事。默认情况下(在我测试时),插入工作表的图像具有LockAspectRatio=True

您需要将此设置为False,否则更改可能无法预测:如果使用 F8 逐步执行代码,则可以观察到Width的更改,但是下一行Height上的宽度将恢复为上一个宽度。

因此,将其设置为false,图像应保留指定的宽度/高度。

Option Explicit
Sub ChangeAllPics()
Dim s As Shape
Dim ws As Worksheet
Set ws = ActiveSheet

For Each s In ActiveSheet.Shapes
    s.LockAspectRatio = msoFalse
    s.Width = 500
    s.Height = 200

Next s
End Sub

答案 1 :(得分:0)

David的答案正是我想要的。我将添加另一件事,在过去约一天的时间里对我有很大帮助。下面的脚本将所有图像拍摄在一张纸上,并以某种方式组织它们,以使所有图像都被放样在另一个图像上,没有一个重叠,并且它们之间都有很小的空间。这使得一切都井井有条,易于遵循。

Sub AutoSpace_Shapes_Vertical()
'Automatically space and align shapes

Dim shp As Shape
Dim lCnt As Long
Dim dTop As Double
Dim dLeft As Double
Dim dHeight As Double
Const dSPACE As Double = 20

  'Set variables
  lCnt = 1

  ActiveSheet.Shapes.SelectAll

  'Loop through selected shapes (charts, slicers, timelines, etc.)
  For Each shp In Selection.ShapeRange
    With shp
      'If not first shape then move it below previous shape and align left.
      If lCnt > 1 Then
        .Top = dTop + dHeight + dSPACE
        .Left = dLeft
      End If

      'Store properties of shape for use in moving next shape in the collection.
      dTop = .Top
      dLeft = .Left
      dHeight = .Height
    End With

    'Add to shape counter
    lCnt = lCnt + 1

  Next shp

End Sub
相关问题