每门编程语言都会遇到操作Excel!本文主要说下Python对Excel操作时合并单元格的情况。
目录
1.效果图
2.用到的方法
3.完整代码
1
效果图
2
用到的方法
Python中操作Excel的库有很多,本次使用 xlsxwriter 来进行写Excel操作。Python版本 3.6。
xlsxwriter 可直接 pip 安装。新建一个Excel,里面可包含多个sheet ,程序中要对具体工作sheet进行操作,这个就像在windows下操作Excel一样。add_worksheet 添加一个sheet
add_format 添加一个样式,比如 居中,居左等
write 写一行数据
merge_range('A1:A7') 合并单元格,A2-A7
set_column 设置列的宽度等
3
完整代码import os
import xlsxwriter
def get_history():
# write excel,construct dict
filename = '333.xlsx'
# 删除结果文件
if (os.path.isfile(filename)):
os.remove(filename)
list = {}
# 注意 python字典里面不能再嵌入字典!
list['name1'] = []
list['name1'].append({
'title': 'title-1-1',
'comments': [{'content': 'comments-name-1-1', 'created_at': '2020-04-13 12:17:10'},{'content': 'comments-name-1-2', 'created_at': '2020-04-13 12:17:10'}]
}
)
list['name1'].append({
'title': 'title-1-2',
'comments': [{