简体   繁体   English

使用openpyxl在两个Excel单元格之间画一条线

[英]Draw a line between two excel cells using openpyxl

I would like to draw a line between the centers of two ( non-adjacent ) cells in an Excel work sheet using openpyxl. 我想在使用openpyxl的Excel工作表中的两个(不相邻)单元格的中心之间画一条线。

Using openpyxl I have created a fairly large lookup table. 使用openpyxl我创建了一个相当大的查找表。 Many of the points in the lookup table are interpolated from a hand full of known points. 查找表中的许多点是从充满已知点的手中插入的。

I would like to draw lines between the cells that were created using the known points. 我想在使用已知点创建的单元格之间画线。 These lines would sort of circle the areas that are interpolated. 这些线可能会圈出要插值的区域。

Expected Result: 预期结果:
(This is the actual Excel generated spread sheet, The lines were added by hand in Excel. I want to automate the line drawing. ) (这是实际的Excel生成的电子表格,这些线是在Excel中手动添加的。我想使线图自动化。)

在此处输入图片说明

In this case the white cells are known data points. 在这种情况下,白色单元格是已知的数据点。 Green(ish) cells are inside of the bounding trianges. 绿色(ish)单元位于边界三角形的内部。 Redish-Blueish are outside. 偏红-偏蓝在外面。 All of data on this sheet was populated via a new sheet using openpyxl. 使用openpyxl通过新工作表填充了此工作表上的所有数据。

The openpyxl documention hints that this is possible but I do not understand how. openpyxl文档暗示这是可能的,但我不知道如何做。

Something along the lines of: 类似于以下内容:

ws.Line['A1':'P17].style['heavy','black']

I think is what I am looking for. 我想这就是我想要的。

[A bit more data ] [更多数据]

Using Excel and win32com I can automate drawing these lines. 使用Excel和win32com,我可以自动绘制这些线条。

line = ws.Shapes.Addline(3,4,70,80).Line

However do to other limitations in Excel I have to create this offline using openpyxl. 但是要限制Excel中的其他限制,我必须使用openpyxl离线创建此文件。 Other_Limitations Other_Limitations
So to re-phrase my question: 因此,重新表述我的问题:
Can openpyxl even draw lines? openpyxl甚至可以画线吗?

I am beginning to think that I will have to create the spreadsheet with openpyxl then open the newly created workbook with Excel and draw the lines with Excel. 我开始认为我将不得不使用openpyxl创建电子表格,然后使用Excel打开新创建的工作簿并使用Excel绘制线条。

I really don't think Excel is suitable for this. 我真的不认为Excel适用于此。 The drawing subsystem uses a completely different coordinate system to the worksheet itself. 绘图子系统使用与工作表本身完全不同的坐标系。 Thus, although it is possible to "anchor" a drawing between two cells, the proportions will be extremely hard to calculate. 因此,尽管可以“锚定”两个单元之间的图形,但是比例将非常难以计算。

I'm sure matplotlib, seaborn or other graphics libraries have tools more suitable for this job. 我确信matplotlib,seaborn或其他图形库具有更适合此工作的工具。

So my original Question was if this can be done with Openpyxl . 所以我最初的问题是,是否可以使用Openpyxl完成。
That question still stands. 这个问题仍然存在。

However here is my solution to draw lines with win32com / Python in Excel directly. 但是,这是我的解决方案,可以直接在Excel中使用win32com / Python绘制线条。
This is not ideal for my situation but it works. 这对于我的情况而言并不理想,但是可以。

def Drawline(Sheet,Start,End):
    StartCell = Sheet.Cells(Start[0],Start[1])
    StartAdjacent = Sheet.Cells(Start[0]+1,Start[1]+1)
    EndCell = Sheet.Cells(End[0],End[1])
    EndAdjacent = Sheet.Cells(End[0]+1,End[1]+1)
    Y1 = ( StartCell.Top + StartAdjacent.Top ) / 2
    X1 = ( StartCell.Left + StartAdjacent.Left ) / 2
    Y2 = ( EndCell.Top + EndAdjacent.Top ) / 2
    X2 = ( EndCell.Left + EndAdjacent.Left ) / 2
    Sheet.Shapes.AddLine(X1,Y1,X2,Y2)

This will draw a line from the center of Start to the center of End on Sheet. 这将在“开始”的中心到“图纸上的结束”的中心之间绘制一条线。

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

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