Python编程快速上手——Excel表格创建乘法表案例分析
作者:授我以驴 时间:2021-06-10 20:45:37
本文实例讲述了Python Excel表格创建乘法表。分享给大家供大家参考,具体如下:
题目如下:
创建程序multiplicationTable.py,从命令行接受数字N,在一个Excel电子表格中创建一个N*N的乘法表。通过命令行窗口传值,例如:
py multiplicationRable.py 6
输出结果为一个6*6的乘法矩阵
思路如下:
- 程序需要做以下事情:
能够从cmd命令行窗口接收参数 n
运行python脚本,生成新的excel文件
excel文件包含n*n的乘法矩阵
- 代码需要做一下事情:
导入openpyxl,sys模块
openpyxl.Workbook()创建新的工作薄对象
get_sheet_by_name取得工作表对象
Font()创建字体样式对象
for循环进行乘法矩阵坐标系设置
for循环进行乘法结果单元格填充
save()方法保存
代码如下:
#! python3
import openpyxl,sys
from openpyxl.styles import Font #输入Font代替输入openpyxl.styles.Font()
newExcel = openpyxl.Workbook() #新建工作簿对象
sheet = newExcel.get_sheet_by_name("Sheet")
n = int(sys.argv[1]) #将sys.argv接收到的参数转换为int型
fontObj = Font(bold = True) #设置字体加粗
for i in range(1,n+1):
sheet.cell(row=i+1,column = 1).value = i #对i+1行,1列进行赋值
sheet.cell(column = i+1,row = 1).value = i #对1行,i+1列进行赋值
sheet.cell(row=i + 1, column=1).font = fontObj #加粗
sheet.cell(column=i + 1, row=1).font = fontObj
# 公式输出
for k in range(2,n+2):
for p in range(2,n+2):
sheet.cell(row =k,column =p).value = (k-1) * (p-1)
newExcel.save("newExcel.xlsx")
运行结果
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/one_Salted_FishGG/article/details/89476793
标签:Python,Excel表格,乘法表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pytorch 数据处理:定义自己的数据集合实例
2021-09-11 06:10:21
python导入模块交叉引用的方法
2021-11-01 07:03:42
ASP无组件汉字验证码
2008-05-08 13:19:00
![](https://img.aspxhome.com/file/UploadPic/up/2008050814170453.gif)
Python+Opencv身份证号码区域提取及识别实现
2021-10-01 17:32:13
![](https://img.aspxhome.com/file/2023/9/112969_0s.jpg)
Windows Server 2008之数据安全保护
2008-12-04 12:49:00
ASP与Excel结合生成数据表和Chart图的代码
2011-03-08 10:50:00
15个用户体验设计剖析
2010-05-11 16:43:00
![](https://img.aspxhome.com/file/UploadPic/20105/11/0901070-61s.jpg)
驯服不听话的网页表格
2007-12-03 11:36:00
Python asyncio异步编程简单实现示例
2023-09-23 15:27:52
![](https://img.aspxhome.com/file/2023/9/68419_0s.jpg)
Python实现PDF转MP3的示例代码
2021-08-07 21:21:06
![](https://img.aspxhome.com/file/2023/2/95152_0s.png)
微软补丁KB967723造成MySQL频繁无法连接
2009-11-03 14:28:00
Windows下Python使用Pandas模块操作Excel文件的教程
2021-01-05 23:07:40
![](https://img.aspxhome.com/file/2023/1/69021_0s.jpg)
MySQL安全性指南 (1)(转)
2010-07-27 12:49:00
pandas loc与iloc用法及区别
2023-01-22 08:26:53
![](https://img.aspxhome.com/file/2023/7/84947_0s.png)
利用Django模版生成树状结构实例代码
2023-11-10 16:41:11
![](https://img.aspxhome.com/file/2023/8/103518_0s.png)
Python使用scrapy采集时伪装成HTTP/1.1的方法
2023-07-07 01:28:40
python-django中的APPEND_SLASH实现方法
2023-04-05 07:17:57
![](https://img.aspxhome.com/file/2023/7/95407_0s.jpg)
操作Windows注册表的简单的Python程序制作教程
2023-01-04 00:17:40
Python 弹窗设计小人发射爱心
2021-07-16 14:41:01
![](https://img.aspxhome.com/file/2023/5/112035_0s.png)
Node.js和PHP根据ip获取地理位置的方法
2023-11-14 21:23:13