一、修改數據類型(中英)
需求:
代碼:
#-*-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()