当前位置: 首页 > news >正文

Python,Spire.Doc模块,处理word、docx文件,极致丝滑

Python处理word文件,一般都是推荐的Python-docx,但是只写出一个,一句话的文件,也没有什么样式,就是36K。

再打开word在另存一下,就可以到7-8k,我想一定是python-docx的问题,但一直没有找到优化方案

这是python-docx生成的文件

from docx import Document# 创建一个新的Word文档
document = Document()# 添加一个段落
document.add_paragraph('Hello, World!')# 保存文档
document.save('hello_world.docx')

最后找到了另外一个模块Spire.Doc,极致丝滑,生成的文件只有7-8k,

模块应该很低调,没见太多人推荐

pip install Spire.Doc

这是Spire.Doc生成的文件,官方的例子写入文件的大概操作都比较全了,而且文字数量,明显是字符多于上面的,但大小7-8k,一整个压缩下来了。

这是文档地址:Python: Create, Read, or Update a Word Document

from spire.doc import *	
from spire.doc.common import *# Create a Document object
doc = Document()# Add a section
section = doc.AddSection()# Set the page margins
section.PageSetup.Margins.All = 40# Add a title
titleParagraph = section.AddParagraph()
titleParagraph.AppendText("Introduction of Spire.Doc for Python")# Add two paragraphs
bodyParagraph_1 = section.AddParagraph()
bodyParagraph_1.AppendText("Spire.Doc for Python is a professional Python library designed for developers to " +"create, read, write, convert, compare and print Word documents in any Python application " +"with fast and high-quality performance.")bodyParagraph_2 = section.AddParagraph()
bodyParagraph_2.AppendText("As an independent Word Python API, Spire.Doc for Python doesn't need Microsoft Word to " +"be installed on neither the development nor target systems. However, it can incorporate Microsoft Word " +"document creation capabilities into any developers' Python applications.")# Apply heading1 to the title
titleParagraph.ApplyStyle(BuiltinStyle.Heading1)# Create a style for the paragraphs
style2 = ParagraphStyle(doc)
style2.Name = "paraStyle"
style2.CharacterFormat.FontName = "Arial"
style2.CharacterFormat.FontSize = 13
doc.Styles.Add(style2)
bodyParagraph_1.ApplyStyle("paraStyle")
bodyParagraph_2.ApplyStyle("paraStyle")# Set the horizontal alignment of the paragraphs
titleParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center
bodyParagraph_1.Format.HorizontalAlignment = HorizontalAlignment.Left
bodyParagraph_2.Format.HorizontalAlignment = HorizontalAlignment.Left# Set the after spacing
titleParagraph.Format.AfterSpacing = 10
bodyParagraph_1.Format.AfterSpacing = 10# Save to file
doc.SaveToFile("output/WordDocument.docx", FileFormat.Docx2019)

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 数据结构-排序的概念、应用及其算法实现1(直接插入排序、希尔排序、选择排序、堆排序、冒泡排序)
  • C++第三十六弹---二叉搜索树的性能飞跃:AVL树原理与实现
  • 钢铁百科:A572Gr60和SA572Gr60材质分析、A572Gr60和SA572Gr60简介
  • 【智能启航,速通电工电子!SmartEDA如何成为你的学习加速器】
  • KCP协议(一)
  • 如何制作微课?微课的制作方法和步骤教程,微课录制软件推荐!
  • 数据依赖,执行顺序,promise,async/await
  • 系统架构师质量属性与架构评估案例分析
  • hive sql 处理多层 json 数组
  • 用R语言运用 Shiny 包打造基于鸢尾花数据集的交互式数据可视化应用
  • 探索Delphi的图形与图像处理能力:从基础到高级应用
  • 获利能力段-新增自定义特性、特性派生增强
  • 手撕初阶数据结构之---排序
  • SpringBoot的使用的注解以及监控监控和lombok使用
  • 数据结构——循环队列
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • android图片蒙层
  • angular学习第一篇-----环境搭建
  • Asm.js的简单介绍
  • DataBase in Android
  • ESLint简单操作
  • HashMap ConcurrentHashMap
  • JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • Linux下的乱码问题
  • Logstash 参考指南(目录)
  • Nginx 通过 Lua + Redis 实现动态封禁 IP
  • orm2 中文文档 3.1 模型属性
  • ubuntu 下nginx安装 并支持https协议
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • Vue--数据传输
  • 安装python包到指定虚拟环境
  • 从零开始的webpack生活-0x009:FilesLoader装载文件
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 函数式编程与面向对象编程[4]:Scala的类型关联Type Alias
  • 回顾 Swift 多平台移植进度 #2
  • 力扣(LeetCode)21
  • 利用jquery编写加法运算验证码
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 职业生涯 一个六年开发经验的女程序员的心声。
  • 函数计算新功能-----支持C#函数
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • ​flutter 代码混淆
  • ​草莓熊python turtle绘图代码(玫瑰花版)附源代码
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • #### go map 底层结构 ####
  • #我与Java虚拟机的故事#连载06:收获颇多的经典之作
  • (19)夹钳(用于送货)
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (八)Docker网络跨主机通讯vxlan和vlan
  • (二)JAVA使用POI操作excel
  • (附源码)springboot家庭财务分析系统 毕业设计641323
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100