如何用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")

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

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

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

相关推荐
郑州光合科技余经理11 小时前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo12311 小时前
matlab画图工具
开发语言·matlab
dustcell.12 小时前
haproxy七层代理
java·开发语言·前端
norlan_jame12 小时前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone12 小时前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ40220549612 小时前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
百锦再12 小时前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
QQ51100828512 小时前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php
QQ_196328847512 小时前
Python-flask框架西山区家政服务评价系统网站设计与开发-Pycharm django
python·pycharm·flask
遥遥江上月12 小时前
Node.js + Stagehand + Python 部署
开发语言·python·node.js