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文本

最新文章

  1. Django进阶(三)
  2. VS web项目 基于IIS调试和模拟域名调试
  3. 学习linux之用mail命令发邮件
  4. android wireshark抓包和fiddler抓包
  5. sql apply
  6. Mybatis的ResultMap的使用
  7. 10 Ways to Inspire Your Team
  8. 跨服务器导入数据SQL语句及其问题解决方案
  9. [RM HA 2] Hadoop 2.0 ResourceManager HA原理
  10. 批量创建IP方法
  11. PROFINET有什么用
  12. vue + spring boot + spring security 前后端分离 携带 Cookie 登录实现 只写了个登录
  13. 约瑟夫环简介,问题以及java实现
  14. c# 抽象类 抽象函数 接口
  15. pyc文件是什么【转载】
  16. BZOJ 1087 互不侵犯King 状态压缩DP
  17. OpenCV 图像旋转实现
  18. Python Flask 多环境配置
  19. XMLHttpRequest使用详解
  20. An internal error occurred during: "Android Library Update".

热门文章

  1. 「Luogu P3395」路障 解题报告
  2. kafka sasl/plain安全认证
  3. 注解@ConfigurationProperties使用方法
  4. Vuex入门实践(中)-多module中的state、mutations、actions和getters
  5. Java8 通关攻略
  6. Spring Cloud(二):Web服务客户端之Ribbon
  7. 引用dll出现的问题:发生一个或多个错误,引用无效或不支持该引用
  8. html转成pdf 下载,支持后台保存
  9. python小功能记录
  10. Nmap使用教程(进阶篇)