python实现生成Word、docx文件的方法分析
作者:longgb123 发布时间:2021-03-01 07:49:53
标签:python,Word,docx
本文实例讲述了python实现生成Word、docx文件的方法。分享给大家供大家参考,具体如下:
http://python-docx.readthedocs.io/en/latest/index.html
生成word的利器!
一、快速开始
from docx import Document
document = Document()
1、段落
加一个段落,下面paragraph 是前面内容的光标指向,后面再该处插入一句话。
paragraph = document.add_paragraph('Lorem ipsum dolor sit amet.')
prior_paragraph = paragraph.insert_paragraph_before('Lorem ipsum')
后面加一句话
paragraph = document.add_paragraph('Lorem ipsum ')
paragraph.add_run('dolor sit amet.')
添加段落风格
document.add_paragraph('Lorem ipsum dolor sit amet.', style='ListBullet')
使用blod、italic 等等
paragraph = document.add_paragraph('Lorem ipsum ')
run = paragraph.add_run('dolor')
run.bold = True
run.italic = True
paragraph.add_run('dolor').bold = True
2、标题
level表示标题的大小
document.add_heading('The role of dolphins', level=2)
3、分页
document.add_page_break()
4、表格
table = document.add_table(rows=2, cols=2)
访问方法:
取出来,单独赋值
cell = table.cell(0, 1)
cell.text = 'parrot, possibly dead'
依然使用二维数组类似的索引。
row = table.rows[1]
row.cells[0].text = 'Foo bar to you.'
row.cells[1].text = 'And a hearty foo bar to you too sir!'
分清楚结构
for row in table.rows:
for cell in row.cells:
print(cell.text)
查看信息
row_count = len(table.rows)
col_count = len(table.columns)
添加一行
row = table.add_row()
动态添加表格
table = document.add_table(1, 3)
# 标题
heading_cells = table.rows[0].cells
heading_cells[0].text = 'Qty'
heading_cells[1].text = 'SKU'
heading_cells[2].text = 'Description'
# 添加内容
for item in items:
cells = table.add_row().cells
cells[0].text = str(item.column1)
cells[1].text = item.column2
cells[2].text = item.column3
5、添加图片
from docx.shared import Inches
document.add_picture('image-filename.png', width=Inches(1.25), height=Inches(1.25))
二、操作document
只能打开07之后的,会覆盖。
document = Document('existing-document-file.docx')
document.save('new-file-name.docx')
打开文件
f = open('foobar.docx', 'rb')
document = Document(f)
f.close()
# or
with open('foobar.docx', 'rb') as f:
source_stream = StringIO(f.read())
document = Document(source_stream)
source_stream.close()
...
target_stream = StringIO()
document.save(target_stream)
三、操作text
段落居中
from docx.enum.text import WD_ALIGN_PARAGRAPH
document = Document()
paragraph = document.add_paragraph()
paragraph_format = paragraph.paragraph_format
paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
左边整体缩进
from docx.shared import Inches
paragraph = document.add_paragraph()
paragraph_format = paragraph.paragraph_format
paragraph_format.left_indent = Inches(0.5)
右边整体缩进
from docx.shared import Pt
paragraph_format.right_indent = Pt(24)
首行缩进
paragraph_format.first_line_indent = Inches(-0.25)
从字体调节,字体大小
run = document.add_paragraph().add_run()
font = run.font
from docx.shared import Pt
font.size = Pt(10.5) # 5号字体
font.italic = True
font.underline = True
字体颜色
from docx.shared import RGBColor
font.color.rgb = RGBColor(0x42, 0x24, 0xE9)
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/longgb123/article/details/53390164


猜你喜欢
- 由于js的代码逻辑越来越重,一个js文件可能会有上千行,十分不利于开发与维护。最近正在把逻辑很重的js拆分成模块,在一顿纠结是使用requi
- 本文实例讲述了python通过ssh-powershell监控windows的方法。分享给大家供大家参考。具体分析如下:对于服务器的监控来说
- 作为k8s官方维护的客户端,k8s go-client对于go语言中使用k8s可以说是唯一选项。但是官方的使用示例我个人觉得并不是很清晰,尤
- #!/usr/local/bin/php -q author:freemouse <?php // 下面是说明. print (&qu
- 页面域关系:主页面a.html所属域A:www.aspxhome.com被iframe的页面b.html所属域B:www.cidianwan
- 一.GO程序目录结构在GOPATH目录下的结构--bin(存放编译后生成的可执行文)|----hello.exe(可执行文件)--pkg(存
- 0. 学习目标栈和队列是在程序设计中常见的数据类型,从数据结构的角度来讲,栈和队列也是线性表,是操作受限的线性表,它们的基本操作是线性表操作
- 本文实例展示了Python生成日历的实现方法。该实例可实现一个月的日历生成5x7的列表,列表里的没个日期为datetime类型,采用pyth
- 在/etc/profile.d/简历oracle.sh内容如下在NLS_LANG设置编码ORACLE_HOME=/usr/lib/oracl
- 可以通过遍历的方法:pandas按行按列遍历Dataframe的几种方式:https://www.jb51.net/article/1726
- 一、简单的多表联查(inner join,left join,right join)1、 两表联查user_table表department
- 前言本文主要讲述numpy数组的计算与转置,讲相同尺寸数组的运算与不同尺寸数组的运算,同时介绍数组转置的三种方法。numpy数组的操作比较枯
- 一、为什么要搭建爬虫代理池在众多的网站防爬措施中,有一种是根据ip的访问频率进行限制,即在某一时间段内,当某个ip的访问次数达到一定的阀值时
- salt分发后,主动将已完成的任务数据推送到redis中,使用redis的生产者模式,进行消息传送#coding=utf-8import f
- 想画一个比较复杂的图像,而且还想用turtle画,最让人想退却的是无规律的笔势和繁多的坐标,但既然没有按奈住冲动的心,那我告诉你一个比较笨的
- 在 IT 开发中,有时我们需要对结构体数组进行排序。Go 语言提供了 sort 包,其中最常用的一种是 sort.Slice() 函数。但是
- mysql使用left join连接出现重复问题描述在使用连接查询的时候,例如以A表为主表,左连接B表,我们期望的是A表有多少条记录,查询结
- Pythond 的函数是由一个新的语句编写,即def,def是可执行的语句--函数并不存在,直到Python运行了def后才存在。函数是通过
- 实这本是说明一个问题 : 每个人在提高自己能力这件事情上, 需要持续不断地努力。以最典型的例子来看,只有通过学习,程序员才能保证不断进步。
- 一、安装SSL证书的环境Apache安装目录:E:phpStudyPHPTutorialApache以上为windows下测试SSL证书安装