Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用openpyxl将xls从xlwt更改为xlsx_Python_Excel_Openpyxl_Xlwt - Fatal编程技术网

Python 使用openpyxl将xls从xlwt更改为xlsx

Python 使用openpyxl将xls从xlwt更改为xlsx,python,excel,openpyxl,xlwt,Python,Excel,Openpyxl,Xlwt,我一直在使用xlwt自动化报告。我必须添加一个COUNTIFS公式,不幸的是它不能与xls中的xlwt一起工作,所以我必须使用openpyxl重新创建整个python脚本。我遇到的问题是,正在提取的数据来自rest API,某些值由“sensor”的前面部分标识。下面的代码显示了我在OpenPYXL中尝试的内容,注释行是我在xlwt中使用的内容。如果有人能告诉我如何让openpyxl像xlwt那样工作,那将是非常棒的 book = Workbook("") sheet = book.active

我一直在使用xlwt自动化报告。我必须添加一个COUNTIFS公式,不幸的是它不能与xls中的xlwt一起工作,所以我必须使用openpyxl重新创建整个python脚本。我遇到的问题是,正在提取的数据来自rest API,某些值由“sensor”的前面部分标识。下面的代码显示了我在OpenPYXL中尝试的内容,注释行是我在xlwt中使用的内容。如果有人能告诉我如何让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")
book=工作簿(“”)
sheet=book.active
工作表[“A1”]=“id”
#工作表。写入(0,列号,'id')
工作表['B1']=“主机名”
#工作表。写入(0,列号,'hostname')
工作表['C1']=“操作系统”
#工作表。写入(0,列号,'os')
工作表['D1']=“ip_地址”
#工作表。写入(0,列编号,“ip地址”)
工作表['E1']=“上次签入时间”
#工作表。写入(0,列编号,“上次签入时间”)
工作表['F1']=“脱机天数”
#工作表。写入(0,列编号,“脱机天数”)
工作表['G1']=“控制台”
#工作表。写入(0,列号,'控制台')
cb=CbResponseAPI()
传感器=列表(cb.选择(传感器))
行=1
对于传感器中的传感器:
#打印传感器
如果sensor.uninstall==False和(sensor.uninstalled==False或sensor.uninstalled==None):
上次签入时间=传感器。上次签入时间。strftime(“%m/%d/%Y”)
p=datetime.now().strftime(“%m/%d/%Y”)
d=datetime\u object=datetime.strtime(上次签入时间,%m/%d/%Y)
q=datetime_object=datetime.strTime(p,“%m/%d/%Y”)
delta=(q-d).天
单元格=工作表。单元格(行=2,列=1)
cell.value=“sensor.id”
单元格=工作表。单元格(行=2,列=2)
cell.value=“sensor.hostname”
#工作表.写入(第0行,sensor.id)
#工作表。写入(第1行,传感器。计算机名称)
如果sensor.os_environment_display_string中的“Windo 7”:
单元格=工作表。单元格(行=2,列=3)
cell.value=“sensor.os\u环境\u显示\u字符串”
#sensor.os\u environment\u display\u string=“Windo 7”
#工作表.写入(第2行,传感器.os\u环境\u显示\u字符串)
sensor.os_environment_display_字符串中的elif“Windo Server 2008”:
单元格=工作表。单元格(行=2,列=3)
cell.value=“sensor.os\u环境\u显示\u字符串”
#sensor.os\u environment\u display\u string=“Windo Server 2008”
#工作表.写入(第2行,传感器.os\u环境\u显示\u字符串)
sensor.os_environment_display_字符串中的elif“Windo Server 2012”:
单元格=工作表。单元格(行=2,列=3)
cell.value=“sensor.os\u环境\u显示\u字符串”
#sensor.os_environment_display_string=“Windo Server 2012”
#工作表.写入(第2行,传感器.os\u环境\u显示\u字符串)
sensor.os_environment_display_字符串中的elif“Windo XP”:
单元格=工作表。单元格(行=2,列=3)
cell.value=“sensor.os\u环境\u显示\u字符串”
#sensor.os\u environment\u display\u string=“Windo XP”
#工作表.写入(第2行,传感器.os\u环境\u显示\u字符串)
sensor.os_environment_display_字符串中的elif“Mac OSX”:
单元格=工作表。单元格(行=2,列=3)
cell.value=“sensor.os\u环境\u显示\u字符串”
#sensor.os\u environment\u display\u string=“Mac OSX”
#工作表.写入(第2行,传感器.os\u环境\u显示\u字符串)
sensor.os_environment_display_字符串中的elif“Windo Server 2003”:
单元格=工作表。单元格(行=2,列=3)
cell.value=“sensor.os\u环境\u显示\u字符串”
#sensor.os\u environment\u display\u string=“Windo Server 2003”
#工作表.写入(第2行,传感器.os\u环境\u显示\u字符串)
sensor.os_environment_display_字符串中的elif“Windo 10”:
单元格=工作表。单元格(行=2,列=3)
cell.value=“sensor.os\u环境\u显示\u字符串”
#sensor.os\u environment\u display\u string=“Windo 10”
#工作表.写入(第2行,传感器.os\u环境\u显示\u字符串)
sensor.os_environment_display_字符串中的elif“Windo 8”:
单元格=工作表。单元格(行=2,列=3)
cell.value=“sensor.os\u环境\u显示\u字符串”
#sensor.os\u environment\u display\u string=“Windo 8”
#工作表.写入(第2行,传感器.os\u环境\u显示\u字符串)
sensor.os_environment_display_字符串中的elif“Windo Server 2016”:
单元格=工作表。单元格(行=2,列=3)
cell.value=“sensor.os\u环境\u显示\u字符串”
#sensor.os_environment_display_string=“Windo Server 2016”
#工作表.写入(第2行,传感器.os\u环境\u显示\u字符串)
其他:
单元格=工作表。单元格(行=2,列=3)
cell.value=“sensor.os\u环境\u显示\u字符串”
单元格=工作表。单元格(行=2,列=4)
cell.value=“传感器网络适配器”
单元格=工作表。单元格(行=2,列=5)
cell.value=“传感器上次签入时间”
单元格=工作表。单元格(行=2,列=6)
#工作表.写入(第2行,传感器.os\u环境\u显示\u字符串)
#工作表。写入(第3行,传感器网络适配器)
#工作表。写入(第4行,上次签入时间)
#工作表。写入(第5行,增量)
#工作表。填写(第6行,“熟食店”)
行+=1

打印(“->DELI-RESPONSE-Done exporting!”好的。我对这方面很在行,但我看到了一些东西。
1.在for循环中,行+=1缩进4个空格太远。它位于if循环内,而不是for循环内。或者更具体地说,它位于底部的else循环内。 2.您有一个变量“row”。但在openpyxl中,“row”是cell的一个属性。因此它是一个虚词。我不确定是否可以将“row”同时用作变量和属性。我会将您的变量更改为“r”。然后在if和elif中