python_docx制作word文档详细使用说明【转】

 

 

目前网上对这一个库的介绍得很少,很零散,所以很多功能我是尽量参考其官网,但是官网上面很多功能目前只有说明文档,而代码并还没有及时更新,以至于按照官网上面做了,python却报错。比如:自定义表格的高度。下面,我对我在此次工作任务中,所遇到的一些基本的功能分别做一下说明与展示。我用的是python2.7

      1.创建文档     

1
2
from docx import Document
document = Document()

若括号里面写入word文件路径,则表明打开该文件

     2.添加标题

1
document.add_heading('This is my title'0)

但是,这个有个问题是标题下面有一条横线,对于重度强迫症的我是无法容忍的。所以我直接添加段落文字表示标题

  3.添加段落文字

1
2
3
4
5
6
7
8
9
from docx.shared import RGBColor
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
document.styles['Normal'].font.name = u'黑体'      #可换成word里面任意字体
= document.add_paragraph()
p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER    #段落文字居中设置
run = p.add_run(u'我添加的段落文字')
run.font.color.rgb = RGBColor(54,95,145)             #颜色设置,这里是用RGB颜色
run.font.size = Pt(36)                 #字体大小设置,和word里面的字号相对应

  4.添加图片

1
pic = document.add_picture('pic.png',width = Inches(1.5))     #图片和python文件不在同一个文件夹下面的时候,要补全文件地址

 默认情况下,图片在文档中是左对齐的,如果要对图片进行居中显示,在网上找了很多方法都不可行,最后找到一种方法是直接加入以下代码:

1
2
last_paragraph = document.paragraphs[-1]
last_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER      #图片居中设置

  5.添加表格

1
table = document.add_table(rows=2, cols=3,style="Table Grid"#添加2行3列的表格

  表格的style有很多种,默认情况下表格是没有边框的,Table Grid格式是普通的黑色边框表格,更多表格样式可以百度。但是,我们很多时候希望对表格进行更为漂亮的修改,比如自定义表格某一列的宽度,表格的高度。

1
2
3
4
from docx.shared import Inches
# table.autofit = False
col = table.columns[1]
col.width = Inches(5)     #设置表格第2列宽度为Inches(5) 默认情况下表格是自动适应文档宽度

  对于表格的高度官网上面有说明文档,但是其库函数的代码没有更新,所以找了很久才找到下面一种方法,以后官网要是更新了代码可以按照官网上面的方法进行设置更为简单一些。

1
2
3
4
5
6
7
8
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
for in range(rows):     #遍历表格的所有行
    tr = table.rows[i]._tr
    trPr = tr.get_or_add_trPr()
    trHeight = OxmlElement('w:trHeight')
    trHeight.set(qn('w:val'), "450")         
    trPr.append(trHeight)                    #表格的每一行进行高度设置,450这个值可以任意修改

  6.表格里面添加文字

1
2
3
4
5
heading_cells = table.rows[0].cells     #将表格的第一行设置为表头
for in range(cols):         #cols为表格的列数
    = heading_cells[i].paragraphs[0]    #利用段落功能添加文字
    run = p.add_run(Arr[i])      #把表头放在一个数组里面的,这样方便赋值
    p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER   #居中设置,默认是左对齐

  还有一种直接对表格赋值的方式:

1
table.cell(i,j).text = u'表格文字'       #在表格的i行j列设置文字,默认文字在表格中是左对齐

  7.添加表格行

1
row = table.add_row()

  若需要对添加的行进行赋值,其方法和上面是一样的。

  8.文档的保存

1
document.save('test.docx')  #可以设置其他路径

  整体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from docx import Document
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
from docx.shared import RGBColor
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Inches
  
  
document = Document()
document.add_heading('This is my title'0)
document.add_paragraph('my paragraph')
  
document.styles['Normal'].font.name = u'黑体'
= document.add_paragraph()
p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
run = p.add_run(u'我添加的段落文字 ')
run.font.color.rgb = RGBColor(5495145)
run.font.size = Pt(36)
  
pic = document.add_picture('logo1.PNG')
last_paragraph = document.paragraphs[-1]
last_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER  # 图片居中设置
  
rows = 2
cols = 3
table = document.add_table(rows=rows, cols=cols,style = "Table Grid")  # 添加2行3列的表格
  
for in range(rows):
    tr = table.rows[i]._tr
    trPr = tr.get_or_add_trPr()
    trHeight = OxmlElement('w:trHeight')
    trHeight.set(qn('w:val'), "450")
    trPr.append(trHeight)  # 表格高度设置
# table.autofit = False
col = table.columns[1]
col.width = Inches(5)
arr = [u'序号',u"类型",u"详细描述"]
heading_cells = table.rows[0].cells
for in range(cols):
    = heading_cells[i].paragraphs[0]
    run = p.add_run(arr[i])
    run.font.color.rgb = RGBColor(5495145)  # 颜色设置,这里是用RGB颜色
    run.font.size = Pt(12)  # 字体大小设置,和word里面的字号相对应
    p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
table.cell(11).text = u'表格文字'
table.add_row()
document.save('test1.docx')

  运行结果如下:

原文地址:https://www.cnblogs.com/jcjc/p/9890526.html