Yohann's blog

Yohann's blog


  • Home

  • Categories

  • Archives

  • Search

Python windows下安装

In Python |

image

Read more »

Python ppt转换pdf

In Python |

image

Read more »

Python excel转json

In Python |

环境

  • Python 3.6.9

  • excel2json (1.0.1)

  • excel2json-3 (0.1.6)

  • openpyxl (3.0.10)

  • xlrd (2.0.1)

开始

  • 安装扩展
1
$ pip3 install excel2json excel2json-3 xlrd==1.2.0 openpyxl
  • 创建 excel_to_json.py,代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#  -*- coding: utf-8 -*-

import json
import openpyxl
import excel2json

def main(orgin_path):
"""
excel转json,提取出excel文件的内容
:param orgin_path: 原始excel的路径
:return: 提取出来的json列表,每个sheet对应一个json文件
"""
# 加载excel
workbook = openpyxl.load_workbook(orgin_path)
# 所有sheet的名字
sheet_list = workbook.sheetnames
# excel转json
excel2json.convert_from_file(orgin_path)
# 对中文内容做处理
json_list = [];
for sheet in sheet_list:
with open(sheet+'.json', 'r') as file_obj:
content = file_obj.read()
with open(sheet+'.json', 'w') as file_obj:
file_obj.write(json.dumps(json.loads(content), ensure_ascii=False))
json_list.append(sheet+'.json')
return json_list

main('origin.xlsx');
  • 执行
1
$ python3 excel_to_json.py

Python excel取消合并单元格

In Python |

环境

  • Python 3.6.9

  • openpyxl (3.0.10)

开始

  • 安装扩展
1
$ pip3 install openpyxl
  • 创建 unmerge_cell.py,代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#  -*- coding: utf-8 -*-
import openpyxl
from copy import deepcopy

def main(orgin_path, target_path):
"""
取消合并后excel的单元格,补充内容,填充时间
:param orgin_path: 原始excel的路径
:param target_path: 修正后excel的路径
:return: 列表
"""
# 加载excel
workbook = openpyxl.load_workbook(orgin_path)
# 所有sheet的名字
sheet_list = workbook.sheetnames
for date in sheet_list:
# 读取每一个工作表
worksheet = workbook[date]
# 获取所有 合并单元格的 位置信息
# 是个可迭代对象,单个对象类型:openpyxl.worksheet.cell_range.CellRange
# 日志记录了excel坐标信息
m_list = worksheet.merged_cells
l = deepcopy(m_list)# 深拷贝
# 拆分合并的单元格 并填充内容
for m_area in l:
# 这里的行和列的起始值(索引),和Excel的一样,从1开始,并不是从0开始(注意)
r1, r2, c1, c2 = m_area.min_row, m_area.max_row, m_area.min_col, m_area.max_col
worksheet.unmerge_cells(start_row=r1, end_row=r2, start_column=c1, end_column=c2)
# 获取一个单元格的内容
first_value = worksheet.cell(r1, c1).value
# 数据填充
for r in range(r1, r2+1):# 遍历行
if c2 - c1 > 0:# 多个列,遍历列
for c in range(c1, c2+1):
worksheet.cell(r, c).value = first_value
else:# 一个列
worksheet.cell(r, c1).value = first_value
workbook.save(target_path)
return sheet_list

orgin_path = 'origin.xlsx'
target_path = 'target.xlsx'
sheet_list = main(orgin_path, target_path)
  • 执行
1
$ python3 unmerge_cell.py

Python SJTU作业整合

In Python |

2020-11-10_5faa3d0d4d6e3.jpeg

Read more »

Python RabbitMQ消息队列

In Python |

WechatIMG91.jpeg

Read more »

Django 《Django基础入门》实验报告

In Django |

WechatIMG15.jpeg

Read more »

Django virtualenv下的环境配置和安装

In Django |

WechatIMG13.jpeg

Read more »

设计模式 适配器模式

In 设计模式 |

WechatIMG29.jpeg

Read more »

设计模式 观察者模式

In 设计模式 |

WechatIMG3.jpeg

Read more »
<1…678…31>
Yohann

Yohann

无知甚多 只力探索

306 posts
25 categories
RSS
GitHub CSDN 简书 博客园
Links
  • super的博客
  • SoTool开发工具
  • 哈希
  • 珊瑚学院
  • 辉哥博客
© 2016 — 2024 Yohann
豫ICP备2024041428号
0%