ValueError:无法将0转换为Excel

时间:2019-04-04 20:56:12

标签: python excel openpyxl

我有一段代码可以将数据从“下载的csv”中复制/粘贴到预定义的excel模板中。某些csv文件必须在粘贴之前进行转置。这在我的PC上没有任何问题,但是在Mac上,它给出了无法将{0!r}转换为Excel“。格式(值)错误。

这是我正在使用的代码:

exec("export SHELL=\"/bin/bash\"; echo 'cd /usr/share/nginx/myproject && /opt/rh/rh-php70/root/usr/bin/php artisan run:task mailer' | /usr/bin/at now +1 minute");

似乎该问题仅发生在需要粘贴之前需要转置的文件上,而其他文件实际上没有问题。

我在这里阅读了其他答案,但是没有任何帮助为我解决了这个问题。任何帮助将不胜感激。

以下是完整的回溯错误:

    def read_transpose(account_id):


    excel_template = load_workbook('Cost_Optimization_Template.xlsx')

    ec2_utilization = excel_template['EC2 RI Utilization ']
    rds_utilization = excel_template['RDS RI Utilization ']
    elasticity = excel_template['Elasticty']
    ec2_newri = excel_template['EC2 New RI']
    coverage = excel_template['RI Coverage']
    accounts = excel_template['Accounts']   
    services = excel_template['Services']
    spot_by_account = excel_template['Spot Usage by Account']
    spot = excel_template['Spot Usage']
    storage = excel_template['Storage']
    storage_by_account = excel_template['Storage by Account']
    cloudwatch_accounts = excel_template['CloudWatch Accounts']
    instance_types = excel_template['Instance Types']
    ri_graph = excel_template['RI Graph']
    instructions = excel_template['Instructions']

    instructions['C5'] = account_id


    tab_list = [ec2_utilization, rds_utilization, elasticity, ec2_newri, coverage, accounts, services, spot_by_account, spot, storage, storage_by_account, cloudwatch_accounts, instance_types, ri_graph]

    #print excel_template.sheetnames    

    file_list = ['ri-subscriptions.csv', 'ri-subscriptions (1).csv', 'costs.csv', 'ec2-recommendations.csv', 'ri-instanceTypes.csv', 'costs_(1).csv', 'costs_(2).csv', 'costs_(3).csv', 'costs_(4).csv', 'costs (5).csv', 'costs (6).csv', 'costs (7).csv', 'costs (8).csv', 'ri-utilization.csv']

    i = 0
    for file in file_list:

        # catching empty file excpetions in case there is no data to read and/or transpose so this tab needs to be skipped


        print (file)


        wb = openpyxl.Workbook()
        ws = wb.active

        # for transposed files
        if file == 'costs_(1).csv' or file == 'costs_(2).csv' or file == 'costs_(3).csv' or file == 'costs_(4).csv' or file == 'costs_(5).csv' or file == 'costs (6).csv' or file == 'costs (7).csv' or file == 'costs (8).csv':

            df = pd.read_csv(file)
            df = df.transpose()

            print ('hit transpose')

            for r in dataframe_to_rows(df, index=False, header=True):
                ws.append(r)

            ws.delete_rows(1) # necessary to delete the index row that gets created when transposing

        else:

            df = pd.read_csv(file)

            print ('hit non transpose')

            for r in dataframe_to_rows(df, index=False, header=True):
                ws.append(r)
    excel_template.save('cost_optimization_template_{0}.xlsx'.format(account_id))

1 个答案:

答案 0 :(得分:0)

原来是转置时创建的标头,而openpyxl似乎不喜欢那里的0,将dataframe_to_rows(df,index = False,header = True)中的r更改为header = False就解决了它