小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

python 提取選擇題到excel

 昵稱163835 2022-09-12 發(fā)布于廣東

今天突然有一個需求,要把統(tǒng)計局網站下載的Word文檔里的表格提取出來,放到Excel表中,便于下一步進行數(shù)據分析。

1. 引入擴展庫

# -*- coding: utf-8 -*-

import docx

from docx import Document

import xlwt;

import xlrd;

import glob

2. 讀取Word文檔中的表格

def readdoc(filename):

doc = docx.Document(filename)

tables = []

for table in doc.tables:

table_temp = []

for row in table.rows:

row_temp = []

for cell in row.cells:

row_temp.append(cell.text)

table_temp.append(row_temp)

tables.append(table_temp)

return tables

3. 寫入Excel文件

def writeExcel(tables,filename):

Sheet_index = 0

workbook = xlwt.Workbook(encoding='utf-8')

for table in tables:

worksheet = workbook.add_sheet('sheet' + str(Sheet_index),cell_overwrite_ok = True)

Sheet_index = Sheet_index + 1

for rows in table:

r = table.index(rows)

for cell in rows:

c = rows.index(cell)

print(r,c,cell)

worksheet.write(r,c,cell)

workbook.save(filename[:-5] + '.xls')

4. 遍歷目錄下所有docx文件,并生成同名Excel文件

filenames = glob.glob('jtdoc/*.docx')

for filename in filenames:

tables = readdoc(filename)

writeExcel(tables,filename)

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約