##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文檔