工作表索引

问题描述 投票:0回答:1

我有一个代码,可以从用户那里获取数据并制作 Excel 表格。 对于每个组,它应该制作自己的工作表,并将其用于“文本”数据。 但它并没有用图像来实现,而是将第一圈的图像放入每个工作表中。 此外,经过所有圈数后,它会删除图像,但我认为这不会成为问题。 这就是代码:

import xlsxwriter
import matplotlib.pyplot as plt
import numpy as np
import os

num = int(input('Input amount of registers: '))
x = 1
x_ind = np.arange(x)

workbook = xlsxwriter.Workbook('Registers.xlsx')

for i in range(num):
    worksheet = workbook.add_worksheet()
    a1 = int(input('Input Power Level 1: '))
    a2 = int(input('Input Min: '))
    a3 = int(input('Input Max: '))
    b1 = int(input('Input Power Level 2: '))
    b2 = int(input('Input Min: '))
    b3 = int(input('Input Max: '))
    c1 = int(input('Input Power Level 3: '))
    c2 = int(input('Input Min: '))
    c3 = int(input('Input Max: '))
    d1 = int(input('Input Power Level 4: ' ))
    d2 = int(input('Input Min: '))
    d3 = int(input('Input Max: '))
    
    row = 0
    
    col1 = workbook.add_format({'bg_color':'red'})
    col2 = workbook.add_format({'bg_color':'green'})
    
    #PW1
    worksheet.write(row, 0, "Input Power Level 1")
    worksheet.write(row, 1, a1)
    worksheet.write(row, 2, "ALARM 1")
    worksheet.write(row, 3, '', col1)
    worksheet.write(row, 4, 'Min Limit')
    worksheet.write(row, 5, a2)
    worksheet.write(row, 6, 'Max Limit')
    worksheet.write(row, 7, a3)
    #Bar1
    pr = 0
    if a2 < 0 and a3 < 0:
        a21 = a2 + (a2*(-1))
        a31 = a3 + (a2*(-1))
        a11 = a1 + (a2*(-1))
        pr = a11/a31 * 100
    elif a2 < 0 and a3 >= 0 and a1 >= 0:
        a21 = a2 + (a2*(-1))
        a31 = a3 + (a2*(-1))
        a11 = a1 + (a2*(-1))
        pr = a11/a31 * 100
    elif a2 < 0 and a3 >= 0 and a1 < 0:
        a21 = a2 + (a2*(-1))
        a31 = a3 + (a2*(-1))
        a11 = a1 + (a2*(-1))
        pr = a11/a31 * 100
    elif a2 >= 0 and a3 >= 0 and a1 >= 0:
        pr = a1/a3 * 100       
    pr = round(pr, 1)
    plt.subplot(1,4,1)
    if a2 < 0 and a3 < 0:
        a22 = a2 + (a2*(-1))
        a32 = a3 + (a2*(-1))
        a12 = a1 + (a2*(-1))
        plt.bar(x_ind, a32, color='white')
        plt.bar(x_ind, a12 , color='purple')
    elif a2 < 0 and a3 >= 0 and a1 >= 0:
        a22 = a2 + (a2*(-1))
        a32 = a3 + (a2*(-1))
        a12 = a1 + (a2*(-1))
        plt.bar(x_ind, a32, color='white')
        plt.bar(x_ind, a12 , color='purple')
    elif a2 < 0 and a3 >= 0 and a1 < 0:
        a22 = a2 + (a2*(-1))
        a32 = a3 + (a2*(-1))
        a12 = a1 + (a2*(-1))
        plt.bar(x_ind, a32, color='white')
        plt.bar(x_ind, a12 , color='purple')
    elif a2 >= 0 and a3 >= 0 and a1 >= 0:
        plt.bar(x_ind, a3, color='white')
        plt.bar(x_ind, a1 , color='purple')

    plt.xticks(x_ind, [a1])
    plt.yticks(x_ind, [pr])
    plt.xlabel('Power Level 1, dBm')
    plt.ylabel('Percent of completion, %')
    row += 1
    
    #PW2
    worksheet.write(row, 0, "Input Power Level 2")
    worksheet.write(row, 1, b1)
    worksheet.write(row, 2, "ALARM 2")
    worksheet.write(row, 3, '', col2)
    worksheet.write(row, 4, 'Min Limit')
    worksheet.write(row, 5, b2)
    worksheet.write(row, 6, 'Max Limit')
    worksheet.write(row, 7, b3)
    #Bar2
    pr = 0
    if b2 < 0 and b3 < 0:
        b21 = b2 + (b2*(-1))
        b31 = b3 + (b2*(-1))
        b11 = b1 + (b2*(-1))
        pr = b11/b31 * 100
    elif b2 < 0 and b3 >= 0 and b1 >= 0:
        b21 = b2 + (b2*(-1))
        b31 = b3 + (b2*(-1))
        b11 = b1 + (b2*(-1))
        pr = b11/b31 * 100
    elif b2 < 0 and b3 >= 0 and b1 < 0:
        b21 = b2 + (b2*(-1))
        b31 = b3 + (b2*(-1))
        b11 = b1 + (b2*(-1))
        pr = b11/b31 * 100
    elif b2 >= 0 and b3 >= 0 and b1 >= 0:
        pr = b1/b3 * 100
    pr = round(pr, 1)
    plt.subplot(1,4,2)
    if b2 < 0 and b3 < 0:
        b22 = b2 + (b2*(-1))
        b32 = b3 + (b2*(-1))
        b12 = b1 + (b2*(-1))
        plt.bar(x_ind, b32, color='white')
        plt.bar(x_ind, b12 , color='purple')
    elif b2 < 0 and b3 >= 0 and b1 >= 0:
        b22 = b2 + (b2*(-1))
        b32 = b3 + (b2*(-1))
        b12 = b1 + (b2*(-1))
        plt.bar(x_ind, b32, color='white')
        plt.bar(x_ind, b12 , color='purple')
    elif b2 < 0 and b3 >= 0 and b1 < 0:
        b22 = b2 + (b2*(-1))
        b32 = b3 + (b2*(-1))
        b12 = b1 + (b2*(-1))
        plt.bar(x_ind, b32, color='white')
        plt.bar(x_ind, b12 , color='purple')
    elif b2 >= 0 and b3 >= 0 and b1 >= 0:
        plt.bar(x_ind, b3, color='white')
        plt.bar(x_ind, b1 , color='purple')

    plt.xticks(x_ind, [b1])
    plt.yticks(x_ind, [pr])
    plt.xlabel('Power Level 2, dBm')
    plt.ylabel('')
    row += 1
    
    #PW3
    worksheet.write(row, 0, "Input Power Level 3")
    worksheet.write(row, 1, c1)
    worksheet.write(row, 2, "ALARM 3")
    worksheet.write(row, 3, '', col1)
    worksheet.write(row, 4, 'Min Limit')
    worksheet.write(row, 5, c2)
    worksheet.write(row, 6, 'Max Limit')
    worksheet.write(row, 7, c3)
    #Bar3 
    pr = 0
    if c2 < 0 and c3 < 0:
        c21 = c2 + (c2*(-1))
        c31 = c3 + (c2*(-1))
        c11 = c1 + (c2*(-1))
        pr = c11/c31 * 100
    elif c2 < 0 and c3 >= 0 and c1 >= 0:
        c21 = c2 + (c2*(-1))
        c31 = c3 + (c2*(-1))
        c11 = c1 + (c2*(-1))
        pr = c11/c31 * 100
    elif c2 < 0 and c3 >= 0 and c1 < 0:
        c21 = c2 + (c2*(-1))
        c31 = c3 + (c2*(-1))
        c11 = c1 + (c2*(-1))
        pr = c11/c31 * 100
    elif c2 >= 0 and c3 >= 0 and c1 >= 0:
        pr = c1/c3 * 100    
    pr = round(pr, 1)
    plt.subplot(1,4,3)
    if c2 < 0 and c3 < 0:
        c22 = c2 + (c2*(-1))
        c32 = c3 + (c2*(-1))
        c12 = c1 + (c2*(-1))
        plt.bar(x_ind, c32, color='white')
        plt.bar(x_ind, c12 , color='purple')
    elif c2 < 0 and c3 >= 0 and c1 >= 0:
        c22 = c2 + (c2*(-1))
        c32 = c3 + (c2*(-1))
        c12 = c1 + (c2*(-1))
        plt.bar(x_ind, c32, color='white')
        plt.bar(x_ind, c12 , color='purple')
    elif c2 < 0 and c3 >= 0 and c1 < 0:
        c22 = c2 + (c2*(-1))
        c32 = c3 + (c2*(-1))
        c12 = c1 + (c2*(-1))
        plt.bar(x_ind, c32, color='white')
        plt.bar(x_ind, c12 , color='purple')
    elif c2 >= 0 and c3 >= 0 and c1 >= 0:
        plt.bar(x_ind, c3, color='white')
        plt.bar(x_ind, c1 , color='purple')

    plt.xticks(x_ind, [c1])
    plt.yticks(x_ind, [pr])
    plt.xlabel('Power Level 3, dBm')
    plt.ylabel('')
    row += 1
    
    #PW4
    worksheet.write(row, 0, "Input Power Level 4")
    worksheet.write(row, 1, d1)
    worksheet.write(row, 2, "ALARM 4")
    worksheet.write(row, 3, '', col2)
    worksheet.write(row, 4, 'Min Limit')
    worksheet.write(row, 5, d2)
    worksheet.write(row, 6, 'Max Limit')
    worksheet.write(row, 7, d3)
    #Bar4
    pr = 0
    if d2 < 0 and d3 < 0:
        d21 = d2 + (d2*(-1))
        d31 = d3 + (d2*(-1))
        d11 = d1 + (d2*(-1))
        pr = d11/d31 * 100
    elif d2 < 0 and d3 >= 0 and d1 >= 0:
        d21 = d2 + (d2*(-1))
        d31 = d3 + (d2*(-1))
        d11 = d1 + (d2*(-1))
        pr = d11/d31 * 100
    elif d2 < 0 and d3 >= 0 and d1 < 0:
        d21 = d2 + (d2*(-1))
        d31 = d3 + (d2*(-1))
        d11 = d1 + (d2*(-1))
        pr = d11/d31 * 100
    elif d2 >= 0 and d3 >= 0 and d1 >= 0:
        pr = d1/d3 * 100    
    pr = round(pr, 1)
    plt.subplot(1,4,4)
    if d2 < 0 and d3 < 0:
        d22 = d2 + (d2*(-1))
        d32 = d3 + (d2*(-1))
        d12 = d1 + (d2*(-1))
        plt.bar(x_ind, d32, color='white')
        plt.bar(x_ind, d12 , color='purple')
    elif d2 < 0 and d3 >= 0 and d1 >= 0:
        d22 = d2 + (d2*(-1))
        d32 = d3 + (d2*(-1))
        d12 = d1 + (d2*(-1))
        plt.bar(x_ind, d32, color='white')
        plt.bar(x_ind, d12 , color='purple')
    elif d2 < 0 and d3 >= 0 and d1 < 0:
        d22 = d2 + (d2*(-1))
        d32 = d3 + (d2*(-1))
        d12 = d1 + (d2*(-1))
        plt.bar(x_ind, d32, color='white')
        plt.bar(x_ind, d12 , color='purple')
    elif d2 >= 0 and d3 >= 0 and d1 >= 0:
        plt.bar(x_ind, d3, color='white')
        plt.bar(x_ind, d1 , color='purple')

    plt.xticks(x_ind, [d1])
    plt.yticks(x_ind, [pr])
    plt.xlabel('Power Level 4, dBm')
    plt.ylabel('')
    
    plt.tight_layout(pad=3.0)
    plt.savefig('Registers.png')
    
    row += 2
    worksheet.insert_image(row, 0, 'Registers.png')
            
workbook.close()  
python xlsxwriter worksheet
1个回答
0
投票

问题在于程序正在使用同名文件覆盖“Registers.png”图像。 XlsxWriter 在文件/工作簿关闭之前不会读取图像,此时图像已被覆盖。

解决方案是为每个唯一的图像指定一个唯一的名称,如下所示:

    # ...
    image_name = f"registers{i}.png"
    plt.savefig(image_name)
    row += 2

另外两个建议:

  1. 应该可以将 matplotlib 图像写入内存缓冲区并将其添加到 XlsxWriter 文件中。
  2. 避免使用 matplotlib 并使用 Excel/XlsxWriter 图表来绘制数据。
© www.soinside.com 2019 - 2024. All rights reserved.