[python-docx]docx文档操作的库
2024-08-23 08:25:17
from docx import Document
from docx.shared import Inches # 新建document对象
document = Document() # 添加段落对象
paragraph = document.add_paragraph("hello world".title()) # 插入段落
paragraph.insert_paragraph_before("Python") # 添加heading
document.add_heading("this is default heading") # 添加换页
# document.add_page_break() # 添加表格
table = document.add_table(rows=2, cols=3)
table.add_row() #添加行 for row in table.rows: # 遍历表格
for cell in row.cells:
cell.text = "fuck"
cell = table.cell(0,0) #单元格 # 添加图像并调整大小
document.add_picture("test.gif", width=Inches(1.0)) # 样式
paragraph = document.add_paragraph("Did i looking better?")
paragraph.style = "ListBullet" # run
paragraph = document.add_paragraph("this is before run test ")
run = paragraph.add_run("this is test run")
run.bold = True
run.style = "Emphasis" # 样式
paragraph.add_run(" run ends hear.") # 保存文档
document.save("test.docx")
以上是新建docx文档
from docx import Document # 创建文档对象(不用关闭)
document = Document("net1.exe.docx") # 遍历段落
with open("test.txt", 'w', encoding="utf-8") as f:
for paragraph in document.paragraphs:
f.write(paragraph.text if paragraph.text else "\n")
以上读取段落并另存为txt文本
最新文章
- Django进阶(三)
- VS web项目 基于IIS调试和模拟域名调试
- 学习linux之用mail命令发邮件
- android wireshark抓包和fiddler抓包
- sql apply
- Mybatis的ResultMap的使用
- 10 Ways to Inspire Your Team
- 跨服务器导入数据SQL语句及其问题解决方案
- [RM HA 2] Hadoop 2.0 ResourceManager HA原理
- 批量创建IP方法
- PROFINET有什么用
- vue + spring boot + spring security 前后端分离 携带 Cookie 登录实现 只写了个登录
- 约瑟夫环简介,问题以及java实现
- c# 抽象类 抽象函数 接口
- pyc文件是什么【转载】
- BZOJ 1087 互不侵犯King 状态压缩DP
- OpenCV 图像旋转实现
- Python Flask 多环境配置
- XMLHttpRequest使用详解
- An internal error occurred during: ";Android Library Update";.