问题
I have a piece of code that copy/pastes data from Downloaded csv's into a predefined excel template. Some of the csv files have to be transposed before being pasted. This works with no issues on my PC, but on a mac it gives a Cannot convert {0!r} to Excel".format(value) error.
Here is the code I am using:
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))
The issue seems to occur only with files that need to be transposed before being pasted as the other ones are actually getting pasted with no issues.
I read through other answers out here but nothing helped solved this issue for me. Any help would be much appreciated.
Below is the full traceback error:
Traceback (most recent call last):
File "cost_optimization.py", line 701, in <module>
read_transpose('111')
File "cost_optimization.py", line 612, in read_transpose
ws.append(r)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/worksheet/worksheet.py", line 654, in append
cell = Cell(self, row=row_idx, column=col_idx, value=content)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/cell/cell.py", line 120, in __init__
self.value = value
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/cell/cell.py", line 252, in value
self._bind_value(value)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/cell/cell.py", line 218, in _bind_value
raise ValueError("Cannot convert {0!r} to Excel".format(value))
ValueError: Cannot convert 0 to Excel
回答1:
Turned out to be the header that got created when transposing, and openpyxl didn't seem to like the 0 in there, changing for r in dataframe_to_rows(df, index=False, header=True) to header=False solved it.
来源:https://stackoverflow.com/questions/55524770/valueerror-cannot-convert-0-to-excel