python读取word表格
#!/usr/bin/python #-*- coding:cp936 -*- from docx import Document from docx.enum.text import WD_PARAGRAPH_ALIGNMENT import re fl_name='/.docx' inpath = 'D:\learn\.docx' inpath2 = 'D:\learn\.docx' name1 = Document(inpath) name2 = Document(inpath2) tb1 = name1.tables[2] # tables=name1.tables row_cells = tb1.rows[0].cells for cell in row_cells: print(cell.text) # # #document = name1('C:\\format.docx') # tables = name1.tables[2] # run = tables.cell(1,0).paragraphs[0].add_run('smida') # run.font.name = '宋体' # run.font.size = 140000 # tables.cell(1,0).paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # name1.save(r"D:\learn\1.docx") doc = [name1,name2] for document in doc: # 修改表格值 for i in [2, 3]: # 修改表格字体格式 tables = document.tables tables[2].add_row() tables[2].cell(i, 0).text = '' tables[2].cell(i, 1).text = '' tables[2].cell(i, 2).text = '' tables[2].cell(i, 3).text = '' tables[2].cell(i, 4).text = '' tables[2].cell(i, 5).text = '' tables[2].cell(i, 6).text = '' tables[2].cell(i, 7).text = '' a = i + 15 a = str(a) c = u'8月' + a + u'日' print(type(c)) print(c) tables = document.tables[2] run1 = tables.cell(i, 0).paragraphs[0].add_run(c) run1.font.name = u'宋体' run1.font.size = 140000 tables.cell(i, 0).paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER print(6666666666666666) print(run1) run2 = tables.cell(i, 1).paragraphs[0].add_run(u'×') run2.font.name = u'宋体' run2.font.size = 140000 tables.cell(i, 1).paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER run3 = tables.cell(i, 2).paragraphs[0].add_run(u'×') run3.font.name = u'宋体' run3.font.size = 140000 tables.cell(i, 2).paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER run4 = tables.cell(i, 3).paragraphs[0].add_run(u'×') run4.font.name = u'宋体' run4.font.size = 140000 tables.cell(i, 3).paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER run5 = tables.cell(i, 4).paragraphs[0].add_run(u'×') run5.font.name = u'宋体' run5.font.size = 140000 tables.cell(i, 4).paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER run6 = tables.cell(i, 5).paragraphs[0].add_run(u'×') run6.font.name = u'宋体' run6.font.size = 140000 tables.cell(i, 5).paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER run7 = tables.cell(i, 6).paragraphs[0].add_run(u'×') run7.font.name = u'宋体' run7.font.size = 140000 tables.cell(i, 6).paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER run8 = tables.cell(i, 7).paragraphs[0].add_run(u'×') run8.font.name = u'宋体' run8.font.size = 140000 tables.cell(i, 7).paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER print(123) print(document) name1.save(inpath) name2.save(inpath2)