前两天让统计所有人员的疫苗接种情况,表格不复杂,都是姓名,身份证号,接种第几针一类的,但是身份证号复制粘贴就会改变,很麻烦,就想到了Python。
代码很简单,就是把名称为1的word文档里的表格导出来,另存为Excel格式。废话不说,代码如下:
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 6 08:24:24 2021
@author: Administrator
"""
from docx import Document
from openpyxl import Workbook
import pandas as pd
document = Document('1.docx')
total = len(document.tables)
for index in range(0, total):
table = []
for row in document.tables[index].rows:
line = []
for grid in row.cells:
line.append(grid.text)
table.append(line)
ws.append(line)
count = count + 1
print("第", count, "个表格正在处理...剩余", total - count + 1, "个表格", "\n")
tables.append(table)
for i in tables:
line = pd.DataFrame(i)
tables = pd.concat([line])
tables.to_excel('导出.xlsx',encoding="utf_8_sig")
tables.head()