Python编程快速上手——让繁琐工作自动化第十二章实践题

##12.13.1

#此脚本用于在Excel文件中,生成一个乘法表,启动时输入模板:python 1207.py 9(乘法表中的最大基数)
import openpyxl
from openpyxl.utils import get_column_letter
from openpyxl.styles import Font
from sys import argv

shuzhi = argv[1]   #提取用户输入的数值
wb = openpyxl.Workbook()
sheet = wb.get_active_sheet()

geshi = Font(bold=True)   #设定格式,字体加粗

for i in range(1, int(shuzhi) + 1):   #循环提取数值
    sheet['A' + str(i + 1)] = i   #对A列中不同行数的单元格进行赋值
    sheet['A' + str(i + 1)].font = geshi   #更改单元格格式为字体加粗
    
    lie1 = get_column_letter(i + 1)   #转换列数为字母
    sheet[lie1 + str(1)] = i   #对第1行中不同列数的单元格进行赋值
    sheet[lie1 + str(1)].font = geshi   #更改单元格格式为字体加粗

for a in range(2, sheet.max_row + 1):   #循环提取工作表中行数
    for b in range(2, sheet.max_column + 1):   #循环提取工作表中列数
        lie2 = get_column_letter(b)   #转换列数为字母
        sheet[lie2 + str(a)] = '=' + lie2 + str(1) + '*' + 'A' + str(a)   #对单元格内容赋值,值为计算函数

wb.save('abc.xlsx')

##12.13.2

#此脚本用于Excel文档中插入空白行,启动时输入模板:python 1208.py 3(本次插入起始行) 2(插入的空白行数量) aaa.xlsx
import openpyxl
from openpyxl.utils import get_column_letter
from sys import argv

py, charu_hanghao, charu_hangshu, wendang = argv
wb = openpyxl.load_workbook(wendang)
sheet = wb.get_active_sheet()

for a in range(sheet.max_row, int(charu_hanghao) - 1, -1):   #确认工作表中内容中最大行数,并进行倒序循环提取
    hang = a + int(charu_hangshu)   #复制单元格后需粘贴的行数
    for b in range(1, sheet.max_column + 1):   #确认工作表中内容最大列数,并进行循环提取
        lie = get_column_letter(b)   #转换列数为字母
        sheet[lie + str(hang)] = sheet[lie + str(a)].value   #从下至上循环复制工作表中单元格内容至新的行列中

for c in range(int(charu_hanghao), int(charu_hanghao) + int(charu_hangshu)):   #确认需清空单元格内容的行数
    for d in range(sheet.max_column, 0, -1):   #确认需清空单元格内容的列数
        lie = get_column_letter(d)   #转换列数为字母
        sheet[lie + str(c)] = None   #清空单元格内容

wb.save('wendang_new.xlsx')

##12.13.3

#此脚本用于将Excel文档中内容行列进行翻转,启动时输入模板:python 1209.py aaa.xlsx
import openpyxl
from openpyxl.utils import get_column_letter
from sys import argv

py, wendang = argv
wb = openpyxl.load_workbook(wendang)
sheet = wb.get_active_sheet()

list_sheet = []   #工作表的列表
max_row    = sheet.max_row      #确定工作表中内容最大行数,并定为全局变量
max_column = sheet.max_column   #确认工作表中内容最大列数,并定为全局变量

for a in range(1, max_row + 1):
    list_hang = []   #每行单元格内容的列表
    for b in range(1, max_column + 1):
        lie = get_column_letter(b)
        coll = sheet[lie + str(a)].value
        list_hang.append(coll)   #将单元格内容添加至列表中,生成每行单元格内容的列表形式
        sheet[lie + str(a)] = None   #清空单元格内容
    list_sheet.append(list_hang)   #将每行单元格内容的列表,添加至工作表的列表中

for c in range(1, max_row + 1):
    lie = get_column_letter(c)   #翻转行数为列数
    for d in range(1, max_column + 1):
        sheet[lie + str(d)] = list_sheet[c - 1][d - 1]   #对翻转位置后的单元格重新赋值
    
wb.save('File_new.xlsx')

##12.13.4

#此脚本用于将多份txt文档中内容导入一份Excel文档中,启动时输入模板:python 1211.py c:\users\lucky\biancheng\wenjian\ aaa1.txt aaa2.txt aaa3.txt ...
import os, re, openpyxl
from sys import argv
from openpyxl.utils import get_column_letter

mulu = argv[1]   #提取用户输入的目录信息
list_txt = argv[2:]   #提取用户输入的txt文档名称
list_txt_read = []   #所有txt文档中所有内容的列表

for a in list_txt:   #遍历所有txt文档
    list_a = open(mulu + a,).readlines()   #提取txt文档中内容为列表形式,文档中每一行均为列表中的一个值
    list_txt_read.append(list_a)   #将txt文档内容的列表,添加至总列表中

wb = openpyxl.Workbook()   #创建一个新的Excel文档
sheet = wb.get_active_sheet()   #获取活跃工作表

for b in range(1, len(list_txt_read) + 1):   #确认txt文档总数,循环提取作为Excel中列数信息
    lie = get_column_letter(b)   #转换列数为字母
    for c in range(1, len(list_txt_read[b-1]) + 1):   #确认每份txt文档中参数数量,循环提取作为Excel中行数信息
        sheet[lie + str(c)] = list_txt_read[b-1][c-1].rstrip('\n')   #提取总列表中对应参数,去除末尾的换行符,赋值给单元格

wb.save(mulu + 'File.xlsx')

##12.13.5

#此脚本用于将一份Excel文档中内容导入多份txt文档中,启动时输入模板:python 1212.py c:\users\lucky\biancheng\wenjian\  File.xlsx
import openpyxl
from sys import argv
from openpyxl.utils import get_column_letter

py, mulu, File = argv   #提取用户输入的目录及操作文件信息
wb = openpyxl.load_workbook(mulu + File)
sheet = wb.get_active_sheet()
list_sheet_read = []   #Excel工作表中所有内容的列表

for a in range(1, sheet.max_column + 1):   #确认工作表中列数,循环提取
    list_a = []   #按列生成临时列表
    lie = get_column_letter(a)   #转换列数为字母
    for b in range(1, sheet.max_row + 1):   #确认工作表中行数,循环提取
        list_a.append(sheet[lie + str(b)].value)   #将列中每行单元格的值添加至此列的临时列表中
    list_sheet_read.append(list_a)   #将每列的值添加至工作表的总列表中

for c in range(1, len(list_sheet_read) + 1):   #确认总列表中值的长度(即工作表中列数),并循环提取
    txt_name = 'File' + str(c) + '.txt'   #创建对应的txt文档(工作表中有多少列,就会创建多少个文档)
    txt = open(mulu + txt_name, 'w')   #打开创建的文档,设定为写入模式
    coll = '\n'.join(list_sheet_read[c - 1])   #将总列表中每个列表的值,以换行符连接为字符串
    txt.write(coll)   #将字符串写入对应的txt文档中
    txt.close()   #保存并关闭txt文档
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值