简体   繁体   English

如何使用python使用Excel绘制图表?

[英]How to draw a chart with excel using python?

Hi I intend to draw a chart with data in an xlsx file. 嗨,我打算在xlsx文件中绘制包含数据的图表。 In order to keep the style, I HAVE TO draw it within excel. 为了保持样式,我必须在excel中绘制它。 I found a package named win32com, which can give a support to manipulate excel file with python on win32 platform, but I don't know where is the doc..... 我找到了一个名为win32com的软件包,该软件包可以支持在win32平台上使用python操作excel文件,但是我不知道文档在哪里.....

Another similar question is how to change the style of cells, such as font, back-color ? 另一个类似的问题是如何更改单元格的样式,例如字体,背景色? So maybe all I wanna know is the doc, you know how to fish is more useful than fishes.... and an example is better. 所以也许我只想知道这个文档,您知道怎么钓鱼比钓鱼更有用....而且一个例子更好。

Check this lib, almost native excel graph can be generated here. 检查这个库,几乎可以在此处生成本机的excel图形。

xlsxwriter line chart example xlsxwriter折线图示例

The only catch here is that you can't update the already existing sheet, means you can modify the sheet, with this lib you can create all most all the charts. 这里唯一的问题是您无法更新已经存在的工作表,这意味着您可以修改工作表,有了这个lib,您可以创建所有几乎所有的图表。

Documentation for win32com is next to non-existent as far I know. 据我所知,win32com的文档几乎不存在。 However, I use the following method to understand the commands. 但是,我使用以下方法来了解命令。

  1. MS-Excel 微软Excel

    In Excel, record a macro of whatever action you intend to, say plotting a chart. 在Excel中,记录要执行的任何操作的宏,例如绘制图表。 Then go to the Macro menu and use View Macro to get the underlying commands. 然后转到“宏”菜单,然后使用“查看宏”获取基本命令。 More often than not, the commands used would guide you to the corresponding commands in python that you need to use. 通常,所使用的命令将引导您转到需要使用的python中的相应命令。

  2. Pythonwin Pythonwin

    You can use pythonwin to browse the underlying win32com defined objects (in your case Microsoft Excel Objects). 您可以使用pythonwin浏览基础的win32com定义的对象(在您的情况下为Microsoft Excel对象)。 In pythonwin (which can be found at \\Lib\\site-packages\\pythonwin\\ in your python installation), go to Tools -> COM Makepy Utility, select your required Library (in this case, Microsoft Excel 14.0 Object Library) and press Ok. 在pythonwin(可在python安装的\\Lib\\site-packages\\pythonwin\\中找到)中,转到“工具”->“ COMp实用程序”,选择所需的库(在这种情况下为Microsoft Excel 14.0对象库),然后按“确定” 。 Then when the process is complete, go to Tools -> COM Browser and open the required library under Registered Libraries. 然后,在该过程完成后,转到“工具”->“ COM浏览器”,然后在“注册库”下打开所需的库。 Note the ID no. 注意ID号 as this would correspond to the source file. 因为这将与源文件相对应。 You can browse the various components of the library in the COM Browser. 您可以在COM浏览器中浏览库的各种组件。

  3. Source 资源

    Go to \\Lib\\site-packages\\win32com\\ in your python installation folder. 转到python安装文件夹中的\\Lib\\site-packages\\win32com\\ Run makepy.py and choose the required library. 运行makepy.py并选择所需的库。 After this, the source file of the library can be found at \\Lib\\site-packages\\win32com\\gen_py . 之后,可以在\\Lib\\site-packages\\win32com\\gen_py找到该库的源文件。 It is one of those files with the wacky name. 它是名称古怪的文件之一。 The name corresponds to that found in Pythonwin. 该名称与在Pythonwin中找到的名称相对应。 Open the file, and search for the commands you saw in the Excel Macro. 打开文件,并搜索在Excel宏中看到的命令。 (#2 and #3 maybe redundant, I am not sure) (我不确定#2和#3可能是多余的)

AFAIK, win32com is an interface to Microsoft's COM technology , so you'd have to look there for an explanation of what objects and methods are available for the different COM-enabled applications. AFAIK,win32com是Microsoft COM技术的接口,因此您必须在此处查找有关可用于支持COM的不同应用程序的对象和方法的说明。 Your task will probably be easier if you don't start with an empty spreadsheet and try to create everything using win32com from scratch, but to create a template in Excel which only needs the actual data filled in. Actualy, since Excel can use ODBC data sources, maybe a database is an even better interface? 如果您不是从一个空的电子表格开始,而是尝试使用win32com从头开始创建所有内容,而是在Excel中创建仅需要填写实际数据的模板,则您的任务可能会更容易。实际上,因为Excel可以使用ODBC数据来源,也许数据库是一个更好的接口?

import win32com.client as w32

oe = w32.Dispatch("Excel.Application")
ow = oe.Workbooks.Add()
osh = ow.Sheets(1)
osh.Select()
osh.Activate()
osh.Cells(1,1).Font.Bold = True # set A1 to bold
osh.Range("A1").Interior.Color = RGB(255,0,0) # set to red background color

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM