简体   繁体   English

使用openpyxl将文本框添加到Excel图表

[英]Adding a text box to an excel chart using openpyxl

I'm trying to add a text box to a chart I've generated with openpyxl, but can't find documentation or examples showing how to do so. 我正在尝试将文本框添加到我使用openpyxl生成的图表中,但无法找到显示如何执行此操作的文档或示例。 Does openpyxl support it? openpyxl支持吗?

I'm not sure what you mean by "text box". 我不确定你的意思是“文本框”。 In theory you can add pretty much anything covered by the DrawingML specification to a chart but the practice may be slightly different. 从理论上讲,您可以将DrawingML规范所涵盖的任何内容添加到图表中,但实践可能略有不同。

However, there is definitely no built-in API for this so you'd have to start by creating a sample file and working backwards from it. 但是,肯定没有内置的API,因此您必须首先创建一个示例文件并从中向后工作。

I also haven't been able to figure out how to do this via OpenPyXL, but you can add a textbox using XLSXWriter . 我也无法通过OpenPyXL弄清楚如何做到这一点,但你可以使用XLSXWriter添加一个文本框。

Note that you can use both OpenPyXL and XLSXWriter at the same time, but it is important to complete the changes you are making in OpenPyXL and save/close the workbook before re-opening it in XLSXWriter and adding the textbox. 请注意,您可以同时使用OpenPyXL和XLSXWriter,但在XLSXWriter中重新打开并添加文本框之前,完成您在OpenPyXL中所做的更改并保存/关闭工作簿非常重要。 (Otherwise, stating the obvious, you could save over any changes (eg the textbox) made by the second package.) (否则,说明显而易见的,您可以保存第二个包所做的任何更改(例如文本框)。)

FWIW, on my laptop at least, the test workbook opens waaaaaay faster in XLSXWriter than it does in OpenPyXL. FWIW,至少在我的笔记本电脑上,测试工作簿在XLSXWriter中比在OpenPyXL中更快地打开了waaaaaay。 A workbook with 10 single-page worksheets, crammed with formulas, formatting, data - takes 8 secs to load in OpenPyXL and about .003 secs to load in XLSXWriter. 一个包含10个单页工作表的工作簿,其中包含公式,格式,数据 - 在OpenPyXL中加载需要8秒,在XLSXWriter中需要大约.003秒。 So re-opening the workbook a second time is not that big a deal, even if processing dozens of workbooks. 因此,即使处理了数十本工作簿,第二次重新打开工作簿也不是什么大不了的事。

Here is a simple code example for creating a textbox using XLSXWriter: 以下是使用XLSXWriter创建文本框的简单代码示例:

import xlsxwriter as xlwr
xlwrwb = xlwr.Workbook('c:\myworkbook.xlsx')
xlwrsheet = xlwrwb.get_worksheet_by_name(sheet.title)

rrow = 10
ccol = 8
ttxt = "This is an example textbox created by OpenPyXL." 
xlwropts = {
    'width': 400,
    'height':200,
    'fill': {'color': '#EEE8AA'},
    'align': {'vertical': 'middle', 'horizontal': 'center'},
    'font': {'bold': True, 'name': 'Arial', 'size': 16, 'color':'orange'},
}
xlwrsheet.insert_textbox(rrow,ccol,ttxt, xlwropts)
xlwrwb.close()

UPDATE: 更新:

Have been working on this example since posting above code and I stand corrected. 自从发布上面的代码以来,我一直在研究这个例子。 The XLSXWriter docs certainly demonstrate adding a textbox as per above code, but XLSXWriter cannot make changes to an existing Excel workbook. XLSXWriter文档肯定会演示如上所述添加文本框,但XLSXWriter无法更改现有的Excel工作簿。 It only can create new workbooks - and it cannot use an existing Excel workbook as a template, even. 它只能创建新的工作簿 - 它甚至不能使用现有的Excel工作簿作为模板。

I will attempt to find another package that can do this, but at this point it doesn't look good. 我将尝试找到另一个可以做到这一点的软件包 ,但此时它看起来不太好。

Here's hoping the good minds at OpenPyXL (Charlie?) can provide a near-enough solution using the DrawingML specification , but at this point that is beyond my own abilities. 这里希望OpenPyXL(Charlie?)的优秀思想可以使用DrawingML规范提供足够接近的解决方案,但此时这超出了我自己的能力范围。

https://xlsxwriter.readthedocs.io/faq.html https://xlsxwriter.readthedocs.io/faq.html

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

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