Quick Start with Python Programming Chapter 13 Practical Projects 13.14.2 Blank Line Insertion Program

topic:

Create a program blankRowInserter.py that takes two integers and a filename string as command-line arguments. Let's call the first integer N and the second integer M. The program is supposed to insert M blank rows into the spreadsheet starting at row N. For example, if the program is executed like this:

python blankRowInserter.py 3 4 myProduce.xlsx

The spreadsheet before and after execution should look like Figure 13-12.

Figure 13-12 Before (left) and after (right) inserting two blank lines in line 3

This program can be written like this: read the contents of the spreadsheet, and then use the for loop to copy the first N rows when writing to a new spreadsheet. For the remaining rows, add M to the row number and write it to the output spreadsheet.

Based on Windows11, python3.11 version, the code according to the requirements of the topic is as follows:

#! python3

import openpyxl, sys, os

# 首先修改目录路径
os.chdir(r'C:\path\to\your')    # 此为你电脑上运行 .py 程序的目录路径
# 即使你要打开的表格和这个.py文件同在这一个目录下,也需要添加这一行才能成功运行代码。

# 获取命令行参数
if len(sys.argv) != 4:
    print('Usage: python blankRowInserter.py N M filename')
    sys.exit()

try:
    n = int(sys.argv[1])
    m = int(sys.argv[2])
except ValueError:
    print('N and M must be integers.')
    sys.exit()

filename = str(sys.argv[3])

# 打开电子表格文件
wb = openpyxl.load_workbook(filename)
sheet = wb.active

# 获取总行数和列数
max_row = sheet.max_row
max_column = sheet.max_column

# 创建新的电子表格
new_wb = openpyxl.Workbook()
new_sheet = new_wb.active

# 拷贝前面的N行
for row in range(1, n + 1):
    for col in range(1, max_column + 1):
        new_sheet.cell(row=row, column=col).value = sheet.cell(row=row, column=col).value

# 写入空行
for row in range(n + 1, max_row + 1):
    for col in range(1, max_column + 1):
        new_sheet.cell(row=row + m, column=col).value = sheet.cell(row=row, column=col).value

# 保存新的电子表格文件
new_wb.save('new_' + filename)
print('Blank rows inserted successfully.')

 Then create a batch file similar to the following:

@py.exe C:\path\to\your\blankRowInserter.py %*
@pause

 Replace this path with the absolute path of your own program and save this file with a .bat file extension (eg blankRowInserter.bat). @pause will add "Press any key to continue..." after the end of the Python script to prevent the program window from disappearing too quickly. At the same time, save this .bat file to the C:\Windows directory.

Then use the command line:

blankRowInserter N M filename

 Among them, N is the line from which to insert blank lines, M is the number of blank lines to be inserted, and filename is the name of the spreadsheet file to be operated. For example:

blankRowInserter 5 6 example.xlsx

The program will generate a new spreadsheet file new_example.xlsx in the same directory, in which blank rows have been inserted.

So far the project is complete.

Of course, the above code is more complicated, you can use openpyxl's own attribute insert_rows to directly insert rows, the code is as follows:

#! python3

import os, sys
from openpyxl import load_workbook

# 首先更改目录路径
os.chdir(r'C:\path\to\your')

# 获取命令行参数
if len(sys.argv) != 4:
    print('Usage: python blankRowInserter.py N M filename')
    sys.exit()

try:
    n = int(sys.argv[1])
    m = int(sys.argv[2])
except ValueError:
    print('N and M must be integers.')
    sys.exit()

filename =  sys.argv[3]

# 打开电子表格文件
wb = load_workbook(filename)
sheet = wb.active

# 插入空行
for i in range(m):    # 遍历循环需要的空行数量,因为是从 0 开始编起,故这里直接用 m
    sheet.insert_rows(n)    # 从第 n 行开始插入 m 行

# 保存新的电子文件表格
wb.save('new_' + filename)
print('Blank rows are inserted successfully.')

 Other operations behind are the same as above.

Guess you like

Origin blog.csdn.net/shufenanbei/article/details/131033105