python-docx编辑word表格

一、修改数据类型(中英)

需求:

代码:

#-*-coding:gbk*-
import os
import docx
#from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.enum.text import WD_ALIGN_PARAGRAPH

filepath = r'f:/1/'
def main():
    docxlist = os.listdir(filepath)
    for mydocx in docxlist:
        newdocx = docx.Document(filepath + str(mydocx))
        #newdocx.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
        table = newdocx.tables
        for oTable in table:
            rows_num = len(oTable.rows)
            columns_num = len(oTable.columns)
            if columns_num >= 5:
                for j in range(rows_num):
                    #English -> Chinese
                    ostring = oTable.cell(j, 3).text
                    oTable.cell(j, 4).paragraphs[0].paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
                    if ostring == 'TIMESTAMP':
                        oTable.cell(j, 3).text = ostring.replace("TIMESTAMP", "时间戳")
                        oTable.cell(j, 4).text = ""
                    elif ostring == 'VARCHAR2':
                        oTable.cell(j, 3).text = ostring.replace("VARCHAR2", "字符串")
                    if ostring == 'DATE':
                        oTable.cell(j, 3).text = ostring.replace("DATE", "日期")
                        oTable.cell(j, 4).text = ""
                    elif ostring == 'NUMBER':
                        oTable.cell(j, 3).text = ostring.replace("NUMBER", "整数")
                    elif ostring == 'FLOAT':
                        oTable.cell(j, 3).text = ostring.replace("FLOAT", "小数")
                #oTable.alignment = WD_TABLE_ALIGNMENT.CENTER
        newdocx.save('f:/2/'+ str(mydocx))
if __name__ == '__main__':
    main()

二、数据类型为日期时,清空位数内容

需求:

代码:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2019/2/20 14:12
# @File    : date_del.py


from docx import Document  # 导入库
"""
word表格中
"""
path = "C:\Users\1\Desktop\福建省质监局标准信息资源目录.docx"  # 文件路径
document = Document(path)  # 读入文件
tables = document.tables  # 获取文件中的表格集
one_cells = []
for table in tables:
    rows_num = len(table.rows)
    columns_num = len(table.columns)
    if rows_num > 2 and columns_num == 4:
        for i in range(1, rows_num):  # 第二行开始
            if table.cell(i, 3).text == '日期' or table.cell(i, 1).text == '日期':
                one_cells.append(table.cell(0, 1).text)
            for j in range(0, columns_num):
                if table.cell(0, 1).text in one_cells:
                    if table.cell(i, j).text == '位数':
                        table.cell(i, j + 1).text = ''

document.save("C:\Users\1\Desktop\1\福建省质监局标准信息资源目录.docx")
# 校验修改内容
for i in one_cells:
    print(i)

三、表格中添加单元格

需求:

参考:https://www.jianshu.com/p/9da61bf35cb7

代码:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2019/2/25 9:52
# @File    : table_add_cell.py


from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import qn
from docx.shared import Pt

"""
word表格,添加一个单元格以及内容
"""
path = "C:\Users\1\Desktop\成都市质量技术监督局标准层信息资源目录.docx"  # 文件路径
document = Document(path)  # 读入文件
# 设置字体为: 宋体
document.styles['Normal'].font.name = u'宋体'
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')

tables = document.tables  # 获取文件中的tables
# 被修改表格的列表
one_cells = []


def match_table():
    """
    遍历文档匹配满足条件的表格
    :return:
    """
    for table in tables:
        rows_num = len(table.rows)
        columns_num = len(table.columns)
        if rows_num > 2 and columns_num == 4:
            one_text = table.cell(0, 1).text
            if len(one_text) == 29:
                one_cells.append(one_text)

                end_text = table.cell(rows_num - 1, columns_num - 1).text
                if end_text:
                    # 添加一行
                    table.add_row()
                    set_font(table, 5, 0)
                else:
                    set_font(table, rows_num - 1, columns_num - 2)
    document.save("C:\Users\1\Desktop\1\python_word_table_add_cell.docx")


def set_font(table, a, b):
    """
    设置表格字体样式
    :param table: 表格
    :param a: 行坐标
    :param b: 列坐标
    :return:
    """
    run = table.cell(a, b).paragraphs[0].add_run(u'信息资源生产格式')  # 内容为:信息资源生产格式
    run.bold = True  # 加粗
    run.font.size = Pt(9)  # 字体大小:小五-9
    table.cell(a, b).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER  # 居中

    run1 = table.cell(a, b + 1).paragraphs[0].add_run(u'ORACLE')
    run1.font.size = Pt(9)
    table.cell(a, b + 1).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER


def check_data():
    # 校验修改内容
    for i in one_cells:
        print(i)


if __name__ == '__main__':
    match_table()
    check_data()
原文地址:https://www.cnblogs.com/fmgao-technology/p/10384593.html