2022.4.25更新:
实现在文件夹中搜索相应字符串并打印的实际功能,后期还需要新建一个数据库总文件夹,把excel报价和带定额的工程项目清单报价分放在不同文件夹中。
import pandas as pd
import os
import glob
# Find all the excel files in the current folder
files = glob.glob('C:\\Users\\PAN\Desktop\\fo1\\*.xlsx')
# Create an empty list to store the DataFrames
dfs = []
search_string = '光束'
# Loop through the files and read each one into a DataFrame
for file in files:
df = pd.read_excel(file)
result = df[df.applymap(lambda x: search_string in str(x)).any(axis=1)]
# 获取文件名并将其添加到 DataFrame 的新列中
filename = os.path.basename(file)
result = result.assign(file_name=filename)
print(result)
dfs.append(result)
file_path2 = 'C:\\Users\\PAN\Desktop\\fo1\\output.xlsx' # double backslashes are used to escape the single backslashes in Windows file paths
sheet_name = 'Sheet1'
combined_df = pd.concat(dfs)
combined_df.to_excel(file_path2, sheet_name=sheet_name, index=False)
——————————————————————————————————
所有报价文件统一格式,这样可以按列搜索项目名称,按行输出信息。
造价文件也可按此进行组织,用带套定额的版本统一输出,这样未来想查一查某个清单是怎么套定额(组价)的,可以直接python检索。
所有价格材料,只要统一格式的表格,都可以快速搜索价格和相关信息。
把某一类报价文件放在一个文件夹中,这样可以用Python进行检索:
import pandas as pd
import glob
# Find all the excel files in the current folder
files = glob.glob('*.xlsx')
# Create an empty list to store the DataFrames
dfs = []
# Loop through the files and read each one into a DataFrame
for file in files:
df = pd.read_excel(file)
dfs.append(df)
# Concatenate all the DataFrames into one
df = pd.concat(dfs)
# Display the result
print(df)