1)cmd模式下:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xlrd
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple openpyxl
2)如果有安装Pycharm,则在程序中操作如下:
菜单栏:File -> Settings -> Project: **.py -> Project INterpreter -> + search xlrd
菜单栏:File -> Settings -> Project: **.py -> Project INterpreter -> + search openpyxl
步骤如下:
1.提取“原报表”第8行往下不为空的数据
2.写入到“抽出数据报表”数组中,设置写入内容的格式及边框、行高等,保存
资源下载:
使用xlrd。读取工作簿,存入wb,然后选取第一个工作表,存入ws。新建一个列表data用于储存获取的数据。然后从第8行(对应索引为0)遍历工作表。将每行的数据放入列表info_list。在写入到数组之前,用if语句设置两个条件:先判断是不是空值,再判断长度是否大于1000mm,才放入总列表data。
import xlrd
file = "汇总.xlsx"
wb = xlrd.open_workbook(file,on_demand=True) # 读取工作簿
ws = wb.sheets()[0] # 选第一个工作表
data = []
title1 = ws.cell(0,0).value.strip() #读取标题,非数字
# print(title1)
for row in range(2, ws.nrows):
col1= ws.cell(row, 0).value
col2 = ws.cell(row, 1).value
col3 = ws.cell(row, 2).value
info_list = [col1, col2, col3]
if info_list[0] != "": # 去除空数据
if col1 >1000:
data.append(info_list)
print(data)
data数据如下:
[[1594.0, 400.0, 38.0], [1045.0, 300.0, 38.0], [2480.0, 149.0, 20.0], [1174.0, 153.0, 20.0], [1450.0, 180.0, 45.0], [1100.0, 90.0, 90.0], [1123.0, 90.0, 90.0], [1155.0, 90.0, 90.0], [1050.0, 53.0, 53.0], [1100.0, 70.0, 45.0], [2050.0, 70.0, 45.0], [2340.0, 70.0, 45.0], [3590.0, 70.0, 45.0]]
获取完数据,开始写入统计表。顺便导入“设置单元格格式”的相关的模块。因为数据列表data中全是子列表,所以直接使用append方法整行写入最为方便。数据写完后,需要设置行高,字号,加粗,对齐,单元格边框等。全部设置完后保存数据。
from openpyxl import load_workbook
from openpyxl.styles import Border, Side, PatternFill, Font, GradientFill, Alignment #设置单元格格式
thin = Side(border_style="thin", color="000000")#定义边框粗细及颜色
file = "统计表模板.xlsx"
wb = load_workbook(file)
ws = wb.active
#写入数据
for i in data:
ws.append(i)
#设置字号,对齐,缩小字体填充,加边框
#Font(bold=True)可加粗字体
for row_number in range(2, ws.max_row+1):
ws.row_dimensions[row_number].height = 25 #设置行高
for col_number in range(1,4):
c = ws.cell(row=row_number,column=col_number)
c.font = Font(size=11,bold=True)
c.border = Border(top=thin, left=thin, right=thin, bottom=thin)
c.alignment = Alignment(horizontal="center", vertical="center")
wb.save("统计表.xlsx")
完整代码如下:
import xlrd
file = "汇总.xlsx"
wb = xlrd.open_workbook(file,on_demand=True) # 读取工作簿
ws = wb.sheets()[0] # 选第一个工作表
data = []
title1 = ws.cell(0,0).value.strip() #读取标题,非数字
# print(title1)
for row in range(2, ws.nrows):
col1= ws.cell(row, 0).value
col2 = ws.cell(row, 1).value
col3 = ws.cell(row, 2).value
info_list = [col1, col2, col3]
if info_list[0] != "": # 去除空数据
if col1 >1000:
data.append(info_list)
print(data)
from openpyxl import load_workbook
from openpyxl.styles import Border, Side, PatternFill, Font, GradientFill, Alignment #设置单元格格式
thin = Side(border_style="thin", color="000000")#定义边框粗细及颜色
file = "统计表模板.xlsx"
wb = load_workbook(file)
ws = wb.active
#写入数据
for i in data:
ws.append(i)
#设置字号,对齐,缩小字体填充,加边框
#Font(bold=True)可加粗字体
for row_number in range(2, ws.max_row+1):
ws.row_dimensions[row_number].height = 25 #设置行高
for col_number in range(1,4):
c = ws.cell(row=row_number,column=col_number)
c.font = Font(size=11,bold=True)
c.border = Border(top=thin, left=thin, right=thin, bottom=thin)
c.alignment = Alignment(horizontal="center", vertical="center")
wb.save("统计表.xlsx")