Python,Excel和图表使用win32com [英] Python, Excel, and Charts using win32com

查看:1454
本文介绍了Python,Excel和图表使用win32com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python和MS Excel问题 - 使用win32com,我可以在Excel中创建一个图表,但我无法在工作表中移动它。我在网上找到代码生成图表:

Python and MS Excel question - using win32com, I'm able to create a chart within Excel but i'm unable to move it within a worksheet. I've found code online to generate the chart:

import win32com.client
from win32com.client import constants as c

xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
xl.Visible = True
wb = xl.Workbooks.Add()
ws = xl.ActiveSheet
ws.Range('A1').FormulaR1C1 = 'X'
ws.Range('B1').FormulaR1C1 = 'Y'
ws.Range('A2').FormulaR1C1 = 1
ws.Range('A3').FormulaR1C1 = 2
ws.Range('A4').FormulaR1C1 = 3
ws.Range('B2').FormulaR1C1 = 4
ws.Range('B3').FormulaR1C1 = 5
ws.Range('B4').FormulaR1C1 = 6
#ws.Range('A1:B4').Select()
ch = ws.Shapes.AddChart().Select()
xl.ActiveChart.ChartType = c.xlXYScatterLines
xl.ActiveChart.SetSourceData(Source=ws.Range("A1:B4"))
#ch.Location(10,10) # something like this?

如何在工作表中移动图表?

How may I move the chart within the worksheet?

推荐答案

您可以指定创建图表时的位置 expression.AddChart(Type,Left,Top,Width,Height) 此处

You can specify the location at the time of creation of the chart expression.AddChart(Type, Left, Top, Width, Height) see here.

如果您不想将图表嵌入工作表,可以创建图表工作表:

If you don't want to embed the chart in a worksheet, you can create a chart sheet :

wb.Worksheets.Add(Type:= xlChart)

.interop.Excel not win32com,但应该可以工作。

Caveat : Microsoft.interop.Excel not win32com, but should work.

这篇关于Python,Excel和图表使用win32com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆