admin管理员组文章数量:1410712
I have a Python script that reads content from JSON file and then write to excel file using pandas with that code and format:
fichero_excel_salida = 'Estad.xlsx'
nombre_pagina = 'Jornada_' + str(num_jornada)
count = 0
# Generamos los dataframe y Volcamos la info al fichero excel
with pd.ExcelWriter(fichero_excel_salida, engine="openpyxl") as writer:
for key,contenido_dict in dict_dict_excels.items():
count = count +1
key = pd.DataFrame(data=contenido_dict)
if (count % 2) == 0:
pass
else:
key = (key.T)
print(key)
if count == 1:
key.style.set_properties(**{'text-align': 'center'}).to_excel(writer, sheet_name=nombre_pagina, startcol=0, startrow=1)
elif count == 2:
key.style.set_properties(**{'text-align': 'center'}).to_excel(writer, sheet_name=nombre_pagina, startcol=0, startrow=6, header=True, index=True)
elif count == 3:
key.style.set_properties(**{'text-align': 'center'}).to_excel(writer, sheet_name=nombre_pagina, startcol=0, startrow=11)
So the result of the code above, give me this with some format cells:
A B C D E F G H I
1
2 10
3 AA 2
4 BB 3
5
6
7 XX YY ZZ
8 AA 5 3 6
9 BB 6 2 6
10
11
12 NUM MIN PTS
13 John 3 24 6
14 Dave 4 12 0
15 Ko 5 30 2
16 Dam 10 20 2
17
Now, I need to get new data from next week, and append this new data to new sheet in the same excel.
What is the best form to do it?
Reading the existing Excel, create new Excel file and put all data again (oldest and newest) using this code?
dict_all_xlsx_file = pd.read_excel(fichero_excel_salida, sheet_name=None)
How I can put the data with the same format as was in the previous excel file (with cell formats)?
Append the new data of each week in the Excel?
How I can do it, because using my Python script in Linux with pandas 2.2.3, all time create new Excel document and I lost old sheets.
I have a Python script that reads content from JSON file and then write to excel file using pandas with that code and format:
fichero_excel_salida = 'Estad.xlsx'
nombre_pagina = 'Jornada_' + str(num_jornada)
count = 0
# Generamos los dataframe y Volcamos la info al fichero excel
with pd.ExcelWriter(fichero_excel_salida, engine="openpyxl") as writer:
for key,contenido_dict in dict_dict_excels.items():
count = count +1
key = pd.DataFrame(data=contenido_dict)
if (count % 2) == 0:
pass
else:
key = (key.T)
print(key)
if count == 1:
key.style.set_properties(**{'text-align': 'center'}).to_excel(writer, sheet_name=nombre_pagina, startcol=0, startrow=1)
elif count == 2:
key.style.set_properties(**{'text-align': 'center'}).to_excel(writer, sheet_name=nombre_pagina, startcol=0, startrow=6, header=True, index=True)
elif count == 3:
key.style.set_properties(**{'text-align': 'center'}).to_excel(writer, sheet_name=nombre_pagina, startcol=0, startrow=11)
So the result of the code above, give me this with some format cells:
A B C D E F G H I
1
2 10
3 AA 2
4 BB 3
5
6
7 XX YY ZZ
8 AA 5 3 6
9 BB 6 2 6
10
11
12 NUM MIN PTS
13 John 3 24 6
14 Dave 4 12 0
15 Ko 5 30 2
16 Dam 10 20 2
17
Now, I need to get new data from next week, and append this new data to new sheet in the same excel.
What is the best form to do it?
Reading the existing Excel, create new Excel file and put all data again (oldest and newest) using this code?
dict_all_xlsx_file = pd.read_excel(fichero_excel_salida, sheet_name=None)
How I can put the data with the same format as was in the previous excel file (with cell formats)?
Append the new data of each week in the Excel?
How I can do it, because using my Python script in Linux with pandas 2.2.3, all time create new Excel document and I lost old sheets.
1 Answer
Reset to default 0It probably would be easiest to append the new data.
Append the new data using:
df.to_excel(writer, sheet_name='New Sheet')
Opening the file in the writer shouldn't override the current information, and specifying the sheet name should avoid the override.
xl = pd.ExcelFile('foo.xls')
xl.sheet_names # see all sheet names
This will get the current sheet names so you can ensure there is no overlap.
Hope this helps.
本文标签: pythonRead content of Excel with multiple sheets with pandas and append new one sheetStack Overflow
版权声明:本文标题:python - Read content of Excel with multiple sheets with pandas and append new one sheet - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744867754a2629445.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
engine="openpyxl"
but use append modemode='a'
which means the workbook must already exist which should be the case. The default mode is 'w' which will overwrite your existing workbook so ensure to set this correctly. Then set the new sheet name. You can also set the'if_sheet_exists'
param to handle what happens if the new sheet name already exists in the workbook – moken Commented Mar 10 at 13:02