如何用python处理excel表格

背景

公司一项业务中,需要将markdown文档,按照一定格式转成excel表格。如果手动复制粘贴,文件数量太多,无趣且耗费时间。所以通过python脚本实现自动转换是最好的选择。

实现途径

经过调研,选择了openpyxl库,这个库安装方便,使用简单。

首先是安装

python 复制代码
pip install openpyxl

然后使用

python 复制代码
from openpyxl import Workbook
wb = Workbook()

# grab the active worksheet
ws = wb.active

# Data can be assigned directly to cells
ws['A1'] = 42

# Rows can also be appended
ws.append([1, 2, 3])

# Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()

# Save the file
wb.save("sample.xlsx")

可以看到,使用起来非常方便。

在转换过程中,需要对原文档按照一定结构进行内容切割和分类,这就是另一个话题了,以后再聊。

关注老刘,学习更多编程知识

相关推荐
ZhengEnCi13 分钟前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi2 小时前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽2 小时前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187913 小时前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L19 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅20 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅20 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L20 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅20 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L21 小时前
python的类&继承
python