当前位置: 首页 > 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)

http://www.lryc.cn/news/420552.html

相关文章:

  • redis的安装与命令
  • 【C++】特殊类设计类型转换
  • 为git 命令行 设置代理环境变量
  • 自定义linux某些常见配置
  • 告别手动操作!KeyMouseGo实现自动化工作流
  • 大型语言模型微调 新进展-4篇 论文
  • 专业课140+杭电杭州电子科技大学843信号与系统考研经验电子信息与通信工程真题,大纲,参考书。
  • php 中 (0 == ‘abc‘) 为真
  • MacOS Anaconda 安装教程及虚拟环境创建
  • Mac快速配置ADB环境变量
  • Kylin的工作原理及使用分享
  • python 使用seleniumwire获取响应数据
  • 用C语言实现双向链表
  • Github 2024-08-10 Rust开源项目日报Top10
  • 深入解析 ESLint 配置:从零到精通
  • BTC连续拉涨,击碎空头幻想
  • 【Spring】Sping笔记01
  • Gridcontrol纵向/横向合并单元格
  • 从周杰伦的《青花瓷》三次更名看方文山的国学情怀与工匠精神
  • HATS:分层图注意力神经网络用于股票预测
  • 【日常记录-MySQL】MySQL设置root用户密码
  • 高级Web安全技术(第二篇)
  • 前端实现文件下载常用几种方式
  • Isaac Lab 安装 (ubuntu22.04环境)
  • todoList清单(HTML+CSS+JavaScript)
  • LVS集群实现四层负载均衡详解(以nat,dr模式为例)
  • 七夕表白网页效果实现与解析
  • 人工智能算法工程师(高级)课程11-自然语言处理之NLP的语言模型-seq2seq模型,seq+注意力与代码详解
  • 从PyTorch官方的一篇教程说开去(6.2 - 张量 tensor 矩阵运算等)
  • 【网络层】直连路由、静态路由、动态路由