学习笔记《Python编程快速上手》实践 12.13.1 乘法表

学习笔记《Python编程快速上手》实践 12.13.1 乘法表避坑请注意:get_highest_column()和get_highest_row() 在新版openpyxl中已经废除。现在的函数是 max

大家好,欢迎来到IT知识分享网。
学习笔记《Python编程快速上手》实践

避坑请注意:

get_highest_column()和get_highest_row() 在新版openpyxl中已经废除。

现在的函数是 max_column 和 max_row。具体用法:

from openpyxl import load_workbook as open wb = open("乘法表.xlsx") sheet = wb.active print(sheet.max_column, sheet.max_row) 

实践项目代码:

import sys import openpyxl from openpyxl import Workbook from openpyxl.utils import get_column_letter from openpyxl.styles import Font # 命令行检测 lenargv = len(sys.argv) if lenargv == 2: try: n = abs(int(sys.argv[1])) except ValueError: print("参数不是整数") elif lenargv == 1: n = 9 else: print("参数最多只有一个") # 建立workbook,改名sheet wb = Workbook() sheet = wb.active sheet.title = "乘法表" # 标题行列 font = Font(b=True) # sz 字号 b 粗体 i 斜体 for i in range(2, n+2): row_A = "A"+str(i) sheet[row_A] = i-1 sheet[row_A].font = font col_1 = get_column_letter(i)+"1" sheet[col_1] = i-1 sheet[col_1].font = font # 乘法表 for i in range(1, n+1): for j in range(1, n+1): sheet[get_column_letter(i+1)+str(j+1)] = sheet["A" + str(i+1)].value*sheet[get_column_letter(j+1)+"1"].value wb.save("乘法表.xlsx") print("%s*%s乘法表已经生成" % (n, n)) 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/58283.html

(0)
上一篇 2024-07-05 12:45
下一篇 2024-07-06 17:26

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信