python_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()

猜你喜欢

转载自www.cnblogs.com/fmgao-technology/p/10384593.html