pip install python-docx 注意不要直接下载docx包

from docx import Document
from docx.shared import RGBColor,Inches,Pt,Length
from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_TAB_ALIGNMENT, WD_TAB_LEADER # 会有红色下划线报异常,不过可以正常使用
from docx.oxml.ns import qn
from docx.oxml import OxmlElement document = Document() # ===============================段落操作================================
document.add_heading('This is my title', 0) # 添加标题,但是这个标题下面会有一个横线 # 设置字体
document.styles['Normal'].font.name = u'黑体' # 可换成word里面任意字体
p = document.add_paragraph() # 设置文字对齐方式
p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER # 段落文字居中设置
# print(p.paragraph_format.alignment) # 打印对齐方式 # 设置段落的颜色及字体大小
run = p.add_run(u'我添加的段落文字')
run.font.color.rgb = RGBColor(54, 95, 145) # 颜色设置,这里是用RGB颜色
run.font.size = Pt(36) # 字体大小设置,和word里面的字号相对应 # ==========缩进 默认左侧=========
paragraph = document.add_paragraph()
paragraph.add_run(
'Indentation is specified using a Length value, such as Inches, Pt, or Cm. Negative values are valid and cause the paragraph to overlap the margin by the specified amount. A value of None indicates the indentation value is inherited from the style hierarchy. Assigning None to an indentation property removes any directly-applied indentation setting and restores inheritance from the style hierarchy:')
paragraph_format = paragraph.paragraph_format
# print(paragraph_format.left_indent)
# paragraph_format.left_indent = Inches(0.5) # 设置为0.5 单位是cm,默认数字为正,向右缩进,为负,则向左移
# print(paragraph_format.left_indent)
# print(paragraph_format.left_indent.inches) # 右侧缩进
# print(paragraph_format.right_indent)
paragraph_format.right_indent = Pt(24)
# print(paragraph_format.right_indent)
# print(paragraph_format.right_indent.pt) # 注意:此时是小写 # 首行缩进
# print(paragraph_format.first_line_indent)
paragraph_format.first_line_indent = Inches(0.25)
# print(paragraph_format.first_line_indent)
# print(paragraph_format.first_line_indent.inches) # ==========制表符==========
"""
制表符停止确定段落文本中制表符的呈现。
特别是,它指定了选项卡字符后面的文本将
开始的位置,它将如何与该位置对齐。
"""
tab_stops = paragraph_format.tab_stops
tab_stop = tab_stops.add_tab_stop(Inches(1.5))
# print(tab_stop.position)
# print(tab_stop.position.inches) # 默认左对齐,但可以通过提供WD_TAB对准枚举领导字符默认为空格,但可以通过提供WD_TAB领导枚举
tab_stop = tab_stops.add_tab_stop(Inches(1.5), WD_TAB_ALIGNMENT.RIGHT, WD_TAB_LEADER.DOTS) # leader为前导符
# print("alignment:",tab_stop.alignment,',leader:',tab_stop.leader) # ==========段落间距==========
# print(paragraph_format.space_before,paragraph_format.space_after)
paragraph_format.space_before = Pt(18) # 单位:磅
# print(paragraph_format.space_before.pt)
paragraph_format.space_after = Pt(12)
# print(paragraph_format.space_after.pt) # ==========行距==========
# print(paragraph_format.line_spacing)
# print(paragraph_format.line_spacing_rule)
paragraph_format.line_spacing = Pt(18) # 固定值18磅
# print(paragraph_format.line_spacing)
# print(paragraph_format.line_spacing_rule)
# paragraph_format.line_spacing = 1.75 # 1.75倍行间距
# print(paragraph_format.line_spacing)
# print(paragraph_format.line_spacing_rule) # ==========分页==========
"""
keep_together导致整个段落出现在同一页上,如果否则会在两页之间中断,则在段落之前发出一个分页。 keep_with_next将段落保存在与下一段相同的页面上。例如,这可以用于将节标题保持在与节的第一段相同的页面上。 page_break_before使段落放置在新页的顶部。这可以在章节标题中使用,以确保章节从新页面开始。 widow_control断开一页,以避免将段落的第一行或最后一行与段落的其余部分放在单独的页面上。
"""
# print(paragraph_format.keep_together)
paragraph_format.keep_with_next = True
# print(paragraph_format.keep_with_next)
paragraph_format.page_break_before = False
# print(paragraph_format.page_break_before) # ===============================添加图片================================
pic = document.add_picture('1.jpg', width=Inches(1.5)) # 图片和python文件不在同一个文件夹下面的时候,要补全文件地址
# 图片默认左对齐,要使图片居中对齐还需要创键一个新的对象
last_paragraph = document.paragraphs[-1] # 段落属性,在这里代表每一行,一共三行,-1为最后一行
last_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER # 图片居中设置 # ===============================添加表格=================================
rows = 3
cols = 3
table = document.add_table(rows=rows, cols=cols, style="Table Grid") # 添加2行3列的表格
"""
表格的style有很多种,默认情况下表格是没有边框的,
Table Grid格式是普通的黑色边框表格,更多表格样式
可以百度。但是,我们很多时候希望对表格进行更为漂
亮的修改,比如自定义表格某一列的宽度,表格的高度。
""" # 设置表格宽度
# table.autofit = False # 关闭表格的自适应宽度,其实与下面两条语句共同执行的话,这条语句可以省略
# col = table.columns[0] # 获取表格第1列
# col.width = Inches(3) # 设置表格第1列宽度为Inches(5) 默认情况下表格是自动适应文档宽度 # 设置表格高度
for i in range(rows): # 遍历表格的所有行
tr = table.rows[i]._tr # 获取表格的每一行
trPr = tr.get_or_add_trPr() # 获取或添加表行属性
trHeight = OxmlElement('w:trHeight') # 获取高度属性
trHeight.set(qn('w:val'), "") # 设置高度
trPr.append(trHeight) # 给表格添加高度属性,表格的每一行进行高度设置,450这个值可以任意修改 # 向表格中添加文字
arr = [u'序号', u'类型', u'详情描述']
arr2 = ['', 'python', '列表']
arr3 = ['', 'java', '数组'] # heading_cells = table.rows[0].cells # 将表格的第一行设置为表头
# row2 = table.rows[1].cells
# for i in range(cols): # cols为表格的列数
#
#
#
# # 表头
# p = heading_cells[i].paragraphs[0] # 利用段落功能添加文字
# run = p.add_run(arr[i]) # 把表头放在一个数组里面的,这样方便赋值
# p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER # 居中设置,默认是左对齐
#
# # 内容第一行
# r2 = row2[i].paragraphs[0]
# r2.add_run(arr2[i])
# r2.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER # 居中设置,默认是左对齐 # 封装成函数
def insert_data(num, cols, list):
"""
:param num: 表格的第几行
:param cols: 表格列数
:param list: 数据
:return:
"""
row = table.rows[num - 1].cells # 获取到表格的某一行
for i in range(cols): # 遍历表格列数
r = row[i].paragraphs[0]
r.add_run(list[i])
r.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER # 居中设置,默认是左对齐 insert_data(1, cols, arr)
insert_data(2, cols, arr2)
insert_data(3, cols, arr3) # 下面两个不推荐使用,这样会造成表格格式的混乱
# 直接给表中的某一个单元格赋值
# table.cell(1, 1).text = 'c' # 在表格的i行j列设置文字,默认文字在表格中是左对齐
# table.cell(1, 2).text = '数组(难)' # 在表格最下方添加行
# table.add_row() document.save('test.docx')

python-docx的更多相关文章

  1. python docx文档转html页面

    文章链接:https://mp.weixin.qq.com/s/uMb2ziRS1NJ1GXIjofeANg 说到word文档转html的,网上一搜一大把,各种在线word转html页面,使用起来也方 ...

  2. Atitit s2018.2 s2 doc list on home ntpc.docx  \Atiitt uke制度体系 法律 法规 规章 条例 国王诏书.docx \Atiitt 手写文字识别 讯飞科大 语音云.docx \Atitit 代码托管与虚拟主机.docx \Atitit 企业文化 每日心灵 鸡汤 值班 发布.docx \Atitit 几大研发体系对比 Stage-Gat

    Atitit s2018.2 s2 doc list on home ntpc.docx \Atiitt uke制度体系  法律 法规 规章 条例 国王诏书.docx \Atiitt 手写文字识别   ...

  3. 读/写docx文件

    安装 pip install python-docx 1.建立新Word文档 建立新文档需要调用Document对象的save方法,一个Document对象代表一个Word文档,该方法的参数是保存的文 ...

  4. pywin32 pywin32 docx文档转html页面 word doc docx 提取文字 图片 html 结构

    https://blog.csdn.net/X21214054/article/details/78873338# python docx文档转html页面 - 程序猿tx - 博客园 https:/ ...

  5. 在QT C++中调用 Python并将软件打包发布(裸机可运行)

    为了提高工作效率,需要一个可以自动生成多份相关联的word文档免去繁琐复制粘贴工作的软件.最后选定使用QT C++做界面和主要逻辑程序设计,对word的操作使用python写好对应的函数,然后在QT中 ...

  6. python 操作word

    pip install python.docx from docx import DocumentDoc = Document() 解释:from 从 docx这个文件中,导入一个叫Document的 ...

  7. 使用PYTHON实现docx文档的读写

    经常写文章的小白们会遇到这样的问题,知道想表达的意思,想出了大概描述的词汇,但就是缺乏完整漂亮的句子,也许曾经在某个地方看到过,但是找不到了.另外一种情况,阅读了大量的报告,用的时候想到了其中的某个结 ...

  8. python 解析docx文档的方法,以及利用Python从docx文档提取插入的文本对象和图片

    首先安装docx模块,通过pip install docx或者在docx官方链接上下载安装都可以 下面来看下如何解析docx文档:文档格式如下 有3个部分组成 1 正文:text文档 2 一个表格. ...

  9. Python:读取 .doc、.docx 两种 Word 文件简述及“Word 未能引发事件”错误

    概述 Python 中可以读取 word 文件的库有 python-docx 和 pywin32. 下表比较了各自的优缺点.   优点 缺点 python-docx 跨平台 只能处理 .docx 格式 ...

  10. Python将数据渲染到docx文档指定位置

    超简单Python将指定数据插入到docx模板渲染并生成 最近有一个需求,制作劳动合同表,要从excel表格中将每个人的数据导入到docx劳动合同中,重复量很大,因此可以使用python高效解决.为了 ...

随机推荐

  1. VMware xp系统联网

    1.

  2. ubuntu下安装intel realsense驱动

    在安装之前一定要确保系统是ubuntu 14.04.3 64位! 由于一开始安装的是32位系统,导致在升级内核版本到4.4时各种问题,最终靠重装系统解决. 因为intel给出的测试代码均是在64位14 ...

  3. ASP.NET MVC + EF 更新的几种方式(超赞)

    1.常用 db.Entry(实体).State = EntityState.Modified;db.SaveChanges(); 2.指定更新 db.Configuration.ValidateOnS ...

  4. 【逆向工具】IDA使用6-签名文件制作

    0x1 签名文件制作的方法: 找到静态编译的程序库 使用IDA中的fair工具包,对静态库操作,生成特征库(IDA6.8 是flair68.zip) 0x2 步骤 第一步:使用pcf生成对应静态库的p ...

  5. Websphere MQ Cluster

    大纲: 1.什么是集群 2.建立一个基本的集群 3.DISPLAY命令 4.负载均衡 5.高级配置和管理 6.答疑 7.关于文章.红宝书等 一. 什么是集群       集群就是Websphere M ...

  6. C++中template的简单用法

    模板(Template)指C++程序设计设计语言中采用类型作为参数的程序设计,支持通用程序设计.C++ 的标准库提供许多有用的函数大多结合了模板的观念,如STL以及IO Stream.使用模板可以使用 ...

  7. 利用mycat实现基于mysql5.5主从复制的读写分离

    整体步骤: 1.准备好两台服务器,一台作为主数据库服务器,一台作为从服务器,并安装好mysql数据库,此处略 2.配置好主从同步 3.下载JDK配置mycat依赖的JAVA环境,mycat采用java ...

  8. bootgrid 刷新保持当前排序

    1. 前言 主要是利用了HTHNL5的localStorage技术和用ajax传输一个数组到后台并进行判断.这篇文章是解决一个小需求而来的,主要是用来记录. 2. 代码 JavaScript: var ...

  9. js实现星级评分效果(非常规5个li代码)

    1. 前言 此方案受到JS单行写一个评级组件启发,自己写了一个简单Demo. 功能有正常滑动,动态显示实心星星个数:当点击确认,则保持当前的实心星星个数:再移动时未点击,则离开后还是保持之前的状态. ...

  10. [Android四大组件之二]——Service

    Service是Android中四大组件之一,在Android开发中起到非常重要的作用,它运行在后台,不与用户进行交互. 1.Service的继承关系: java.lang.Object → andr ...