简体   繁体   English

Openpyxl不会保存文件

[英]Openpyxl won't save file

For some reason Openpyxl won't save the the xlsx file at the end of the program. 由于某种原因,Openpyxl不会在程序末尾保存xlsx文件。 I am trying to read measurments from a file, each line is a different measurement. 我正在尝试从文件中读取测量值,每一行都是不同的度量。 I want to take them and write them to excel as to make using this data later on easier. 我想把它们写成Excel以使以后使用这些数据变得更容易。 Everything seems to work, but in the end the data isn't saved, if i create new file where the changes should be saved it will not be created. 一切似乎都可以正常工作,但最终不会保存数据,如果我在应保存更改的位置创建了新文件,则不会创建该文件。

from openpyxl import load_workbook
from openpyxl import Workbook
wb = load_workbook(filename='Data_Base.xlsx')
sheet = wb.worksheets[0]
BS = []
Signal = []
with open('WifiData2.txt') as f:
 for line in f:
    y = int(line.split('|')[0].split(';')[3])
    x = int(line.split('|')[0].split(';')[2])
    floor = int(x = line.split('|')[0].split(';')[1])
    data = line.split("|")[1].strip()
    measurements = data.split(";")
    for l in measurements:
        raw = l.split(" ")
        BSSID = raw[0]
        signal_strength = raw[1]
        print(signal_strength)
        BS.append(BSSID)
        Signal.append(signal_strength)

    for row_num in range(sheet.max_row):
        num = row_num
        if row_num > 1:
            test_X = int(sheet.cell(row=row_num, column=4).value)
            test_Y = int(sheet.cell(row=row_num, column=3).value)
            test_floor = int(sheet.cell(row=row_num, column=2).value)
            if (test_X == x and test_Y == y and test_floor == floor):
                nr = nr + 1

    if (nr > 3):
        q = 1

    if (q == 0):

        sheet.cell(row=sheet.max_row+1, column = 2, value = floor)
        sheet.cell(row=sheet.max_row + 1, column=3, value=x)
        sheet.cell(row=sheet.max_row + 1, column=4, value=y)
        sheet.cell(row=sheet.max_row + 1, column=2, value=sheet.max_row)
        for element in BS:
            nr = 0
            for col in sheet.max_column:
                if BS[element] == sheet.cell(row=1, column=col).value:
                    sheet.cell(row=sheet.max_row + 1, column=col, value=Signal[element])
                    nr = 1
            if (nr == 0):
                sheet.cell(row=1, column=sheet.max_column+1, value=BS[element])
                sheet.cell(row=sheet.max_row+1, column=sheet.max_column + 1, value=BS[element])

    Signal.clear()
    BS.clear()
wb.save('Data_Base1.xlsx')

What is weird that if i save the workbook earlier it will create the file. 奇怪的是,如果我较早保存工作簿,它将创建该文件。 Of course it doesnt really work for me since any changes that i want made won't be made. 当然,它实际上对我不起作用,因为我要进行的任何更改都不会进行。 I had similar issue when i tried it with xlrd/wt/utils combo. 当我尝试使用xlrd / wt / utils组合时,我遇到了类似的问题。 Does any1 know where the problem is ? 有谁知道问题出在哪里吗?

使用绝对路径而不是相对路径可以解决问题!

Add

wb.template = False

before 之前

wb.save('Filename.xlsx')

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

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