Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pictures.add does not seem to change width and height if called in loop #1491

Closed
nikolasharing opened this issue Jan 18, 2021 · 7 comments · Fixed by #1626
Closed

pictures.add does not seem to change width and height if called in loop #1491

nikolasharing opened this issue Jan 18, 2021 · 7 comments · Fixed by #1626
Labels
Milestone

Comments

@nikolasharing
Copy link

OS: Windows 10

Versions of xlwings, Excel and Python: 0.21.4, Office 365, Python 3.8.2

When I call pictures.add it does not seem to change width and height of the second and further pictures if called in a loop. It could be related to:

https://stackoverflow.com/questions/65688768/why-are-the-properties-height-and-width-not-adjusting-the-picture-size-in-ex

Include a minimal code sample to reproduce the issue (and attach a sample workbook if required!)

plot_size = 200

if plot_bool==True:
    for plot in plots_sheet.pictures: plot.delete()
    for count,plot in enumerate(plot_list):
        plots_sheet.pictures.add(plot, left=count*plot_size,width=plot_size,height=plot_size)

## with the following code, the height and width is actually changed for all pictures:
# for pic in plots_sheet.pictures:
#     pic.height = plot_size
#     pic.width  = plot_size

@fzumstein
Copy link
Member

Can you please change this into a self-contained code snippet? i.e. include the plotting part, imports etc.

@nikolasharing
Copy link
Author

nikolasharing commented Jan 21, 2021

You mean like this?

import xlwings as xw
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(100)
y = np.random.randn(100)
plot_list = []

plots_book  = xw.Book()
plots_sheet = plots_book.sheets[0]

for plot in plots_sheet.pictures: plot.delete()

for i in range(5):
    fig, ax = plt.subplots()
    ax.plot(x,y)
    plot_list.append(fig)

plot_size = 200

for count, plot in enumerate(plot_list):
    plots_sheet.pictures.add(plot, left=count*plot_size,top=100,width=plot_size,height=plot_size)

@fzumstein
Copy link
Member

yes, thanks. This is what I see:

Screen Shot 2021-01-22 at 09 10 33

@nikolasharing
Copy link
Author

Yes exactly, but shouldn't the pictures be non overlapping from the left and width parameters chosen above?

@fzumstein
Copy link
Member

Gotcha, yes, there seems to be an inconsistency in that the width/height parameters of the pictures.add method resize the picture before sending them over to Excel which uses a different unit than Excel.

@igorrev
Copy link

igorrev commented Mar 14, 2021

Gotcha, yes, there seems to be an inconsistency in that the width/height parameters of the pictures.add method resize the picture before sending them over to Excel which uses a different unit than Excel.

Have the same issue. If I do pic = xw.pictures.add(width=80, height=80) image size is wrong. Next commands pic.width=80 and pic.height=80 sets right image size.

So @fzumstein , do you find other solution?

BTW, I think the issue doesn't appear in old-old Excel versions like 2007 and 2010.

@fzumstein
Copy link
Member

Sounds like a good workaround @igorrev - it's not a priority on my end (you can always change that).

@fzumstein fzumstein added this to the 0.23.5 milestone Jun 20, 2021
@fzumstein fzumstein added the bug label Jun 20, 2021
fzumstein added a commit that referenced this issue Jun 20, 2021
- use unique temp files for matplotlib/plotly
- allow to use svg file format
- use svg with matplotlib/plotly
- fix issues with wrong size
- move plotly from pro to open source
closes #1624, closes #1491
fzumstein added a commit that referenced this issue Jun 20, 2021
- use unique temp files for matplotlib/plotly
- allow to use svg file format
- use svg with matplotlib/plotly
- fix issues with wrong size
- added lock_aspect_ratio
- move plotly from pro to open source
closes #1624, closes #1491, closes #1625
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants