[Python] 纯文本查看 复制代码import math
import warnings
import winreg
import xlwings as xw
import openpyxl
import pandas as pd
from pandas.core.common import SettingWithCopyWarning
import configparser
warnings.simplefilter(action="ignore", category=SettingWithCopyWarning)
wb = openpyxl.Workbook()
wb.create_sheet(index=0, title='单杆备料')
sheets = wb.worksheets # 获取当前所有的sheet
ws = wb.active
ws['A1'] = '织板种类'
ws['B1'] = '尺寸'
ws['C1'] = 'FAW'
ws['D1'].value = '用量'
ws['E1'].value = '数量'
def desktop_path():
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
desktop = winreg.QueryValueEx(key, "Desktop")[0]
# print(desktop)
return desktop
print(desktop_path())
name = "测试.xlsx"
wb.save("{}\{}".format(desktop_path(), name))
# 从配置文件中遍历获取
# 读取配置文件
config = configparser.ConfigParser()
config.read('config.ini')
# 获取 part_numbers 和 quantity 的值
part_numbers = config.get('Settings', 'part_numbers').split(',')
quantity = config.get('shuliang', 'quantity').split(',')
# 初始化计数器
index = 0
# 循环调用两组使用
for i in range(2):
for j in range(len(part_numbers)):
try:
print(f'使用 {part_numbers[index]},数量为 {quantity[index]}')
user = part_numbers[index]
s = int(quantity[index])
print(user, s)
index += 1
except IndexError:
break
print('写入完成')
A0 = 1000
A15 = 518
A19 = 528
A25 = 550
A30 = 570
A37 = 625
A45 = 702
A60 = 570
A90 = 1000
A88 = 1000
ST200675F63 = [[],
["ST2006-75-F", "长度:63in", "版本:F1", "数量::", s],
["ST03910+90°1/2", "94X1565", 88, 1 * s, 94 * 1 * s / A88],
["HC30°", "93X1600", 70, 1 * s, 93 * 1 * s / A30],
["HC90°", "92X1495", 70, 1 * s, 92 * 1 * 1495 * s / A90],
["HC0°", "91X100", 100, 1 * s, 91 * 1 * s / A0],
["HC45°", "91X1215", 70, 1 * s, 91 * 1 * s / A45],
["HC0°", "90X515", 100, 1 * s, 90 * 1 * s / A0],
["HC37°", "90X1600", 70, 1 * s, 90 * 1 * s / A37],
["HC30°", "89X1425", 70, 1 * s, 89 * 1 * s / A30],
["HC0°", "88X1525", 70, 1 * s, 88 * 1 * s / A0],
["HC37°", "87X1600", 70, 1 * s, 87 * 1 * s / A37],
["HC45°", "86X1135", 70, 1 * s, 86 * 1 * s / A45],
["HC0°", "25X1485", 70, 2 * s, 25 * 2 * s / A0],
["HC37°", "85X1615", 70, 1 * s, 85 * 1 * s / A37],
["HC45°", "84X1615", 70, 1 * s, 84 * 1 * s / A45]]
ST200680F63 = [[],
["ST2006-80-F", "长度:63in", "版本:G1", "数量::", s],
["ST03910+90°1/2", "94X1565", 88, 1 * s, 94 * 1 * s / A88],
["HC30°", "93X1600", 70, 1 * s, 93 * 1 * s / A30],
["HC90°", "92X1495", 70, 1 * s, 92 * 1 * 1495 * s / A90],
["HC0°", "91X100", 100, 1 * s, 91 * 1 * s / A0],
["HC45°", "91X1215", 70, 1 * s, 91 * 1 * s / A45],
["HC0°", "90X515", 100, 1 * s, 90 * 1 * s / A0],
["HC37°", "90X1600", 70, 1 * s, 90 * 1 * s / A37],
["HC30°", "89X1425", 70, 1 * s, 89 * 1 * s / A30],
["HC0°", "88X1525", 70, 1 * s, 88 * 1 * s / A0],
["HC37°", "87X1600", 70, 1 * s, 87 * 1 * s / A37],
["HC45°", "86X1135", 70, 1 * s, 86 * 1 * s / A45],
["HC0°", "25X1485", 70, 2 * s, 25 * 2 * s / A0],
["HC37°", "85X1615", 70, 1 * s, 85 * 1 * s / A37],
["HC30°", "84X1615", 70, 1 * s, 84 * 1 * s / A30]]
dict_1 = {'ST2006-75-F-63': ST200675F63, 'ST2006-80-F-63: ST200680F63}
h = dict_1.get(user)
if user in dict_1.keys():
name = "测试.xlsx"
workbook = openpyxl.load_workbook("{}\{}".format(desktop_path(), name)) # 返回一个workbook数据类型的值
sheet = workbook.active # 获取活动表
print(user + '写入成功!\n当前活动表是:' + str(sheet))
for row in h:
sheet.append(row) # 使用append插入数据
workbook.save("{}\{}".format(desktop_path(), name))
else:
print('输入错误!注意大小写,请检查刚性和型号及长度!或者暂未建立数据!')
df = pd.read_excel("{}\{}".format(desktop_path(), name), sheet_name=0)
pd.set_option('display.unicode.east_asian_width', True)
data = df.groupby(['织板种类', '尺寸', 'FAW'], as_index=False).agg({'用量': 'sum', '数量': 'sum'})
new_data = pd.DataFrame(data)
for k in range(len(new_data)):
if (new_data['数量'][k]) >= 0.1:
new_data['数量'][k] = math.ceil(new_data['数量'][k])
col_df1 = new_data['数量'].astype(int)
df1 = new_data
name2 = '选手杆单杆备料2.xlsx'
df1.to_excel("{}\{}".format(desktop_path(), name2), index=None)
app = xw.App(visible=False, add_book=False) # 启动Excel程序
workbook = app.books.open("{}\{}".format(desktop_path(), name2)) # 打开要调整行高和列宽的工作簿
worksheet = workbook.sheets[0] # 指定工作簿中的第一个工作表
worksheet.autofit() # 自动调整工作表的行高和列宽
workbook.save() # 保存工作簿
workbook.close() # 关闭工工作簿
app.quit() # 退出Excel 程序
我想把ST200675F63 =.........、ST200680F63 =........ 放入一个配置文件里 再把dict_1 =.......... 放入另一个配置文件里,这样的话以后就可以直接进入配置文件添加就好了,就不用改代码了。