ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【Python】【python-docx讲解】

2020-07-04 18:35:04  阅读:285  来源: 互联网

标签:docx Python text cell python doc new table


参考:https://www.jianshu.com/p/8d8a75a50190

最近因项目需要,需从一个word文件中提取表格中的数据至另外一个word文件的表格中,每次操作都较为麻烦,所以写了一段python代码实现此功能,需要用到python-docx库(pip install python-docx),下面将对其语法进行详解。

1.需要导入的库

from docx import Document(文档读写)

from docx.shared import Pt (字体大小)

from docx.shared import Inches

from docx.oxml.ns import qn(设置字体格式)

from docx.shared import RGBColor (设置字体颜色)

from docx.enum.text import WD_ALIGN_PARAGRAPH (设置对其方式)

 

2.文档读取

doc=Document('文件路径')  #打开word文档

paras=doc.paragraphs   #获取文件段落

tables=doc.tables   #获取文件表格

table=tables[0]  #获取第一个表格

table=tables[0].cell(1,2).text  #获取表格1的第二行第三列的表格内容

—————获取表格1的内容————

for row in table.rows:

for cell in row.cells:

    print(cell.text)

 

3.文档操作

doc=Document() #创建一个空白文档

p1=doc.add_paragraph()  #初始化建立一个自然段

p1.alignment=WD_ALIGN_PARAGRAPH.CENTER  #对齐方式为居中,没有这句话默认左对齐

p1.paragraph_format.line_spacing=1.5  #设置该段落,行间距为1.5倍

p1.paragraph_format.first_line_indent=Inches(0.5)  #段落缩进0.5英寸

p1.paragraph_format.left_line_indent=Inches(0.5)  #设置左缩进0.5英寸

p1.paragraph_format.right_line_indent=Inches(0.5)  #设置右缩进0.5英寸

p1.space_after=Pt(5)  #设置段后距离为5磅

p1.space_before=Pt(5)  #设置段前距离为5磅

 

run1=p1.add_run(u'你好')   #写入文本“你好”

run1.font.size=Pt(12)  #设置字体大小为12

run1.font.bold=True  #设置加粗

run1.italic=True  #设置斜体

 

para_heading1=doc.add_heading('',level=2)  #返回一个2级标题

run2=para_heading=para_heading.add_run(u'前言')  #添加二级标题前言

 

#添加图片

doc.add_picture('照片路径', width=Inches(1.25))

 

#增加表格

table=doc.add_table(rows=3, cols=3, style='Table Grid')  #创建一个3行3列的表格,样式为黑色框

table.cell(0, 0).text=u'你好'  #添加第一行第一列内容为“你好”

table.cell(2,0).merge(table.cell(2,2))  #合并第三行第一列至第三列单元格

table.columns[

 

doc.save(D:\word.docx)  #保存文件在D盘,命名为word.docx

 

此方法虽然可以完成对段落的设置,但是每增加一个段落都要重新设置样式,因此可以设置统一的标题,各个标题都可以采用,具体代码如下所示:代码中 "Normal"表示正文的样式,["Heading 2"]表示2级标题的样式,当然一级标题的样式关键字为["Heading 1"],u代表后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时出现乱码。

 

doc=Document() #创建一个空白文档

doc.styles["Normal"].font.name=u'Times New Roman'  #设置正文字体为Times New Roman

doc.styles["Normal"]._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')  #设置中文字体为宋体

上述两句连用的含义是设置中文字体为宋体,西文字体为Times New Roman

doc.styles["Normal"].font.color.rgb=RGBColor(255,0,0) #设置正文全局颜色为红色

doc.styles["Normal"].font.size=Pt(12) #设置字体大小为12磅

doc.styles["Heading 2"].font.size=Pt(20) #设置全局2级标题的字体大小为20磅

 

直接附上代码

import os
from docx import Document
from docx.oxml.ns import qn
from docx.shared import Pt

class File_transfer_to_wordtable():
    """提取文件内容至新的word"""
    def __init__(self, file_path):
        self.file_path = file_path

    def tables(self):
        """读取文件,获取文件中的表格"""
        file_exist = os.path.exists(self.file_path)
        if file_exist:
            document=Document(self.file_path) #打开"文件"
            tables=document.tables  #获取文件中的表格集
            return tables
        else:
            return "此文件不存在!"


    def new_document(self):
        """
        在新文件中新建表格,插入表头
        """
        tables_num = len(self.tables())
        new_document = Document()  # 创建文档对象
        new_document.styles["Normal"].font.size = Pt(7)
        new_document.styles['Normal'].font.name = 'Times New Roman'
        new_document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
        new_table = new_document.add_table(rows=tables_num-1, cols=6, style='Table Grid')

        new_table.cell(0, 0).text = u"序号"
        new_table.cell(0, 1).text = u"变更活动编号"
        new_table.cell(0, 2).text = u"需求和故障描述"
        new_table.cell(0, 3).text = u"涉及更改说明"
        new_table.cell(0, 4).text = u"更改波及影响分析"
        new_table.cell(0, 5).text = u"用例选取"

        """读取变更说明中的第3个到最后一个表的内容"""
        for i in range(2, tables_num):
            table = self.tables()[i]
            cell_1 = table.cell(0, 1).text
            cell_2 = table.cell(1, 1).text
            cell_3 = table.cell(5, 1).text
            cell_4 = table.cell(6, 1).text
            cell_34 = "变更文件:"+ cell_3 +"\n"+ "变更模块:" + cell_4
            cell_5 = table.cell(9, 1).text
            cell_6 = table.cell(11, 1).text

            """将表中内容填入另一个word中"""
            new_table.cell(i-1, 0).text = str(i-1)
            new_table.cell(i-1, 1).text = cell_1
            new_table.cell(i-1, 2).text = cell_2
            new_table.cell(i-1, 3).text = cell_34
            #若涉及到的测试用例为空,则写入无
            if cell_5 == "":
                new_table.cell(i-1, 4).text = "无"
            else:
                new_table.cell(i-1, 4).text = cell_5
            new_table.cell(i-1, 5).text = cell_6

        """保存word文档"""
        new_document.save("D:\\file\word.docx")

#创建实例 word=File_transfer_to_wordtable("D:\\file\xxx.docx") print(word.tables()) word.new_document()

 

标签:docx,Python,text,cell,python,doc,new,table
来源: https://www.cnblogs.com/yuhuang/p/13236053.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有