如何用python創建excel表格?
代碼示例:
import pandas as pd
'''
pip install openpyxl xlwt pandas
'''
def create_Excel():
# 按行插入數據,可以再write_data中append([]),
write_data = [
['1212',1,2,3]
]
write_data.append(['1211',1111,2,3])
# 表頭格式
excel_list = ['id','A','B','C']
df = pd.DataFrame(write_data,
columns=excel_list)
xlsx_path = 'data_excel.xlsx'
# 生成表格對象
writer = pd.ExcelWriter(xlsx_path)
# 寫數據,sheet_name:Excel名
df.to_excel(writer, sheet_name='完整性統計', index=False)
df2 = pd.DataFrame(write_data,
columns=excel_list)
df2.to_excel(writer, sheet_name='表2', index=False)
# 按列插入數據
df3 = pd.DataFrame({
'id':[123,122,121],
'姓名':['丁一','丁二','丁三'],
'年齡':[18,18,18]
}, columns=['id','姓名','年齡'])
df3.to_excel(writer, sheet_name='性別年齡', index=False)
# 保存數據
writer.save()