87 lines
2.7 KiB
Python
87 lines
2.7 KiB
Python
import pandas as pd
|
|
import io
|
|
import csv
|
|
|
|
def parse_section(section):
|
|
lines = section.strip().split('\n')
|
|
if len(lines) < 2:
|
|
return pd.DataFrame() # Return empty DataFrame if there's no data
|
|
|
|
data_lines = lines[1:]
|
|
data = list(csv.reader(data_lines))
|
|
if len(data) <= 1:
|
|
return pd.DataFrame() # Return empty DataFrame if there's no data
|
|
|
|
df = pd.DataFrame(data[1:], columns=data[0])
|
|
return df
|
|
|
|
def process_txt(file_path):
|
|
with open(file_path, 'r', encoding='utf-8') as file:
|
|
content = file.read()
|
|
|
|
objects = content.split('#########################################################')
|
|
all_settings = []
|
|
|
|
for obj in objects:
|
|
if obj.strip() == "":
|
|
continue
|
|
|
|
sections = obj.split('# ')
|
|
if len(sections) < 8:
|
|
continue
|
|
|
|
obj_name = sections[1].strip().split('\n')[0].replace("Object: ", "").strip()
|
|
print(f"Processing object: {obj_name}")
|
|
|
|
try:
|
|
basic_info = sections[3].strip()
|
|
settings = sections[5].strip()
|
|
assignments = sections[7].strip()
|
|
|
|
print(f"\nRaw Basic Info for {obj_name}:\n{basic_info}")
|
|
print(f"\nRaw Settings for {obj_name}:\n{settings}")
|
|
print(f"\nRaw Assignments for {obj_name}:\n{assignments}")
|
|
|
|
basic_info_df = parse_section(basic_info)
|
|
settings_df = parse_section(settings)
|
|
assignments_df = parse_section(assignments)
|
|
|
|
print(f"\nBasic Info DataFrame for {obj_name}:")
|
|
print(basic_info_df)
|
|
|
|
print(f"\nSettings DataFrame for {obj_name}:")
|
|
print(settings_df)
|
|
|
|
print(f"\nAssignments DataFrame for {obj_name}:")
|
|
print(assignments_df)
|
|
|
|
if not settings_df.empty:
|
|
settings_df['Profile Name'] = obj_name
|
|
all_settings.append(settings_df)
|
|
else:
|
|
print(f"Settings DataFrame is empty for {obj_name}")
|
|
except Exception as e:
|
|
print(f"Error processing object {obj_name}: {e}")
|
|
continue
|
|
|
|
if all_settings:
|
|
all_settings_df = pd.concat(all_settings, ignore_index=True)
|
|
return all_settings_df
|
|
else:
|
|
print("No settings were processed successfully.")
|
|
return pd.DataFrame()
|
|
|
|
# Path to the settings.txt file
|
|
file_path = 'settings.txt'
|
|
|
|
# Process the file and consolidate the settings
|
|
consolidated_settings_df = process_txt(file_path)
|
|
|
|
if not consolidated_settings_df.empty:
|
|
# Export to Excel
|
|
output_file = 'consolidated_policies_from_txt.xlsx'
|
|
consolidated_settings_df.to_excel(output_file, index=False)
|
|
print(f"Consolidated policies have been exported to {output_file}")
|
|
else:
|
|
print("No data to export.")
|