其他
python实现存货批量计价测试
import os
for root, dirs, files in os.walk("存货收发存文件夹"):
for file in files:
with open('name.txt','a') as f:
f.write(file+'\n')
f.close()
如果是获取代码所在的当前目录的所有文件列表,则使用os.listdir()函数
import os
for file in os.listdir('.'):
with open('当前目录文件列表.txt','a') as f:
f.write(file+'\n')
f.close()
第二步:利用for函数通过读取name.txt文件对1-12月存货收发存明细表进行插列处理,在首列插入月份列,并把月份列所有行设置为月份数,对指定列数进行空白值的向后填充-利用pd.read_excel读取sheet,利用df.insert进行插列处理,利用ffill函数向后填充
import pandas as pd
import os
f=open('name.txt',encoding='utf-8')
names=f.readlines()
lists=[]
for name in names:
lists.append(name.replace('\n',''))
for i in range(1,13):
df1 = pd.read_excel(lists[i-1],'Sheet1')
df1.insert(0,'月份',str(i))
print(list(df1))
col=['Unnamed: 0','Unnamed: 1']
df1[col]=df1[col].ffill()
df1.to_excel(str(i)+'_'+lists[i-1])
import pandas as pd
import os
f=open('name.txt',encoding='gbk')
names=f.readlines()
lists=[] #用来装载插入月份列后的每月收发存数据表的名称
lists1=[] #用来装载插入月份列后的每月收发存数据
for name in names:
lists.append(name.replace('\n',''))
for i in range(len(lists)):
object=pd.read_excel(lists[i],'Sheet1')
lists1.append(object)
df = pd.concat(lists1) #合并1-12月收发存数据
df.to_excel(r'res-12个月.xlsx', 'Sheet1', index=None)
f=open('存货测试名称.txt',encoding='utf-8')
names=f.readlines()
list=[] #用来装载期末需进行计价测试的存货名称
new_list=[] #用来装载拼接月份和名称后的唯一标识符
for name in names:
name=name.replace('\n','')
list.append(name)
for i in range(len(list)):
for j in range(1,13):
new_name=str(j)+'-'+list[i]
new_list.append(new_name)
for i in range(len(new_list)):
with open('new_name.txt','a') as f1:
f1.writelines(new_list[i]+'\n')
import openpyxl as op
file='计价测试模板.xlsx'
wb=op.load_workbook(file)
ws=wb['模板']
wb1=op.load_workbook('样本收发存数据.xlsx',data_only=True)
ws1=wb1['数据']
f=open('存货测试名称.txt',encoding='utf-8')
names=f.readlines()
lists=[]
for name in names:
lists.append(name.replace('\n',''))
f1=open('索引号.txt',encoding='utf-8')
suoyins=f1.readlines()
lists1=[]
for suoyin in suoyins:
lists1.append(suoyin.replace('\n',''))
#print(names)
for i in range(1,11): #本次编程思源君选取了10个存货计价测试样本
object=wb.copy_worksheet(wb['模板'])
object.title=str(i)
object.cell(3,14).value=lists1[i-1]
object.cell(7,2).value=lists[i-1]
object.cell(11,9).value=ws1.cell(2+12*(i-1),3).value #期初数量
object.cell(11,11).value=ws1.cell(2+12*(i-1),5).value #期初金额
for k in range(1,13):
object.cell(k+11,3).value=ws1.cell(k+1+12*(i-1),6).value #本期增加数量
object.cell(k+11,5).value=ws1.cell(k+1+12*(i-1),8).value #本期增加金额
object.cell(k+11,6).value=ws1.cell(k+1+12*(i-1),9).value #本期发出数量
object.cell(k+11,8).value=ws1.cell(k+1+12*(i-1),11).value #本期发出金额
wb.save(file)
import openpyxl as op
file='计价测试模板.xlsx'
wb1=op.load_workbook(file)
ws1=wb1['汇总表']
for i in range(1,11): #本次编程思源君选取了10个存货计价测试样本
wb2=op.load_workbook(file,data_only=True)
ws1.cell(i+7,5).value=wb2[str(i)].cell(23,9).value #期末数量
ws1.cell(i+7,6).value=wb2[str(i)].cell(23,10).value #期末单价
ws1.cell(i+7,7).value=wb2[str(i)].cell(23,11).value #期末余额
ws1.cell(i+7,8).value=wb2[str(i)].cell(24,16).value #测算差异
wb1.save('计价测试汇总.xlsx')
感谢您阅读“思源审计”,如有点滴收获,请点击下方链接关注