简体   繁体   English

使用openpyxl将xls从xlwt更改为xlsx

[英]Changing xls to xlsx from xlwt with openpyxl

I have been using xlwt to automate reports. 我一直在使用xlwt来自动执行报告。 I have to add a COUNTIFS formulas which unfortunately does not work with xlwt in xls so I have to recreate the entire python script using openpyxl. 我必须添加一个COUNTIFS公式,不幸的是,该公式不适用于xls中的xlwt,因此我必须使用openpyxl重新创建整个python脚本。 The problem I am having is that the data being pulled is from an rest API and certain value's are identified with a preceding of "sensor." 我遇到的问题是,要提取的数据来自其他API,并且某些值以“传感器”的开头标识。 The below code show's what I am trying with open pyxl and the commented rows are what I was using in xlwt. 下面的代码显示了我在尝试使用pyxl的过程,注释行是我在xlwt中使用的行。 If anyone can tell me how to make the openpyxl do what the xlwt was doing that would be awesome. 如果有人可以告诉我如何使openpyxl做xlwt所做的事情,那将是很棒的。

book = Workbook("")
sheet = book.active

sheet["A1"] = "id"
#worksheet.write(0, column_number, 'id')

sheet['B1'] = "hostname"
#worksheet.write(0, column_number, 'hostname')

sheet['C1'] = "os"
#worksheet.write(0, column_number, 'os')

sheet['D1'] = "ip_address"
#worksheet.write(0, column_number, 'ip_address')

sheet['E1'] = "last_checkin_time"
#worksheet.write(0, column_number, 'last_checkin_time')

sheet['F1'] = "days_offline"
#worksheet.write(0, column_number, 'days_offline')

sheet['G1'] = "console"
#worksheet.write(0, column_number, 'console')

cb = CbResponseAPI()    
sensors = list(cb.select(Sensor))


row = 1

for sensor in sensors:

    #print sensor

    if sensor.uninstall == False and (sensor.uninstalled == False or sensor.uninstalled == None):

        last_checkin_time = sensor.last_checkin_time.strftime('%m/%d/%Y')
        p = datetime.now().strftime('%m/%d/%Y')


        d = datetime_object = datetime.strptime(last_checkin_time, '%m/%d/%Y')
        q = datetime_object = datetime.strptime(p, '%m/%d/%Y')

        delta = (q - d).days

        cell = sheet.cell(row=2, column=1)
        cell.value = "sensor.id"
        cell = sheet.cell(row=2, column=2)
        cell.value = "sensor.hostname"
        #worksheet.write(row, 0, sensor.id) 
        #worksheet.write(row, 1, sensor.computer_name) 

        if "Windo 7" in sensor.os_environment_display_string:

            cell = sheet.cell(row=2, column=3)
            cell.value = "sensor.os_environment_display_string"
            #sensor.os_environment_display_string = "Windo 7"
            #worksheet.write(row, 2, sensor.os_environment_display_string) 

        elif "Windo Server 2008" in sensor.os_environment_display_string:

            cell = sheet.cell(row=2, column=3)
            cell.value = "sensor.os_environment_display_string"
            #sensor.os_environment_display_string = "Windo Server 2008"
            #worksheet.write(row, 2, sensor.os_environment_display_string)

        elif "Windo Server 2012" in sensor.os_environment_display_string:

            cell = sheet.cell(row=2, column=3)
            cell.value = "sensor.os_environment_display_string"
            #sensor.os_environment_display_string = "Windo Server 2012"
            #worksheet.write(row, 2, sensor.os_environment_display_string) 

        elif "Windo XP" in sensor.os_environment_display_string:

            cell = sheet.cell(row=2, column=3)
            cell.value = "sensor.os_environment_display_string"
            #sensor.os_environment_display_string = "Windo XP"
            #worksheet.write(row, 2, sensor.os_environment_display_string) 

        elif "Mac OSX" in sensor.os_environment_display_string:

            cell = sheet.cell(row=2, column=3)
            cell.value = "sensor.os_environment_display_string"
            #sensor.os_environment_display_string = "Mac OSX"
            #worksheet.write(row, 2, sensor.os_environment_display_string) 

        elif "Windo Server 2003" in sensor.os_environment_display_string:

            cell = sheet.cell(row=2, column=3)
            cell.value = "sensor.os_environment_display_string"
            #sensor.os_environment_display_string = "Windo Server 2003"
            #worksheet.write(row, 2, sensor.os_environment_display_string) 

        elif "Windo 10" in sensor.os_environment_display_string:

            cell = sheet.cell(row=2, column=3)
            cell.value = "sensor.os_environment_display_string"
            #sensor.os_environment_display_string = "Windo 10"
            #worksheet.write(row, 2, sensor.os_environment_display_string) 

        elif "Windo 8" in sensor.os_environment_display_string:

            cell = sheet.cell(row=2, column=3)
            cell.value = "sensor.os_environment_display_string"
            #sensor.os_environment_display_string = "Windo 8"
            #worksheet.write(row, 2, sensor.os_environment_display_string) 

        elif "Windo Server 2016" in sensor.os_environment_display_string:

            cell = sheet.cell(row=2, column=3)
            cell.value = "sensor.os_environment_display_string"
            #sensor.os_environment_display_string = "Windo Server 2016"
            #worksheet.write(row, 2, sensor.os_environment_display_string) 

        else:
            cell = sheet.cell(row=2, column=3)
        cell.value = "sensor.os_environment_display_string"
        cell = sheet.cell(row=2, column=4)
        cell.value = "sensor.network_adapters"
        cell = sheet.cell(row=2, column=5)
        cell.value = "sensor.last_checkin_time"
        cell = sheet.cell(row=2, column=6)




        #worksheet.write(row, 2, sensor.os_environment_display_string) 
        #worksheet.write(row, 3, sensor.network_adapters) 
        # worksheet.write(row, 4, last_checkin_time)
        # worksheet.write(row, 5, delta)
        # worksheet.write(row, 6, "DELI") 

        row+=1

print("-> DELI-RESPONSE - Done exporting! <-")

book.save("sensor_export.xlsx")

Ok. 好。 I'm quite an amateur at this, but here are a couple of things I see. 我对此很业余,但是我看到了几件事。
1. In your for loop, the row += 1 is indented 4 spaces too far. 1.在for循环中,行+ = 1缩进了4个空格。 It is inside the if loop, not the for loop. 它位于if循环内,而不是for循环内。 Or more specifically, it is inside the else loop at the bottom. 更具体地说,它位于底部的else循环内。 2. You have a variable "row". 2.您有一个变量“行”。 But in openpyxl, "row" is an attribute of cell. 但是在openpyxl中,“行”是单元的属性。 So it is a functional word. 因此,这是一个实用的词。 I'm not sure if you can make it work by using "row" as both a variable and as an attribute. 我不确定是否可以通过将“ row”用作变量和属性来使其工作。 I would change your variable to maybe "r". 我会将您的变量更改为“ r”。 Then inside the if's and elif's, you can have: cell = sheet.cell(row=r, column=3). 然后在if和elif的内部,可以有:cell = sheet.cell(row = r,column = 3)。
As the code is right now, you are telling openpyxl that the row = 2 in every instance. 就像现在的代码一样,您要告诉openpyxl每个实例中的row = 2。

Good luck. 祝好运。

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

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