zoukankan      html  css  js  c++  java
  • 在word中读取表格和写入表格

     1 from docx import Document
     2 
     3 word =Document(r'表格名称.docx')
     4 
     5 #读取表格
     6 
     7 tables=word.tables
     8 
     9 for i in tables[:]:
    10 
    11   for j,row in enumerate(i.rows[:]):
    12 
    13     row_content=[]
    14 
    15     for cell in row.cells[:]  :   #读取每行中的所有单元格
    16 
    17       c=cell.text
    18 
    19       row_content.append(c)
    20 
    21     print(row_content)    #打印一行的信息
    22 
    23 
    24 
    25 #写入表格
    26 
    27 data=[
    28 
    29 ['学号','姓名','成绩'],
    30 
    31 [101,'李四',95],
    32 
    33 [102,'张三',90],
    34 
    35 [103,'王五',86]
    36 
    37 ]
    38 
    39 table=word.add_table(rows=4,cols=3)
    40 
    41 for row  in range(4):
    42 
    43   cells=table.rows[row].cells
    44 
    45    for  col  in   range(3):
    46 
    47     cells[col].text=str(data[row][col])
  • 相关阅读:
    DZY Loves Sequences
    Boredom easy dp
    floyd算法 poj1125
    poj3259 Bellman_Ford算法
    poj1860 Bellman_Ford算法
    Java 获取资源文件路径
    CLion 2020.1.2 激活
    Kotlin学习笔记
    Kotlin Hello World
    WebStorm 2020.1.2 激活
  • 原文地址:https://www.cnblogs.com/luckiness/p/13080392.html
Copyright © 2011-2022 走看看