Here is an idea for a program that I would like to create:
Get a list of all excel files in a specific folder and its subfolders, which could also be a FOlder on a different Server given as a UNC-Path. the list needs to hav the name, path,size,author, owner, create datetime, last edited, last accessed.
The list should also contain the name and source of the data connections used in the Excel-file.
It should also show the names of all sheets and their respective rowcount and how many pivot charts it contains.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To create a program that fulfills your requirements, you can use Python with the os, openpyxl, and win32com.client libraries. The os library will help you navigate the file system, openpyxl will allow you to read Excel files, and win32com.client will enable you to interact with Excel files on a remote server via COM.
Here's a sample Python script that demonstrates how to achieve this:
python
import os
import win32com.client
from datetime import datetime
def get_excel_file_info(path):
excel_files = []
for root, dirs, files in os.walk(path):
for file in files:
if file.lower().endswith('.xlsx'):
excel_files.append(os.path.join(root, file))
excel_file_info = []
for excel_file in excel_files:
try:
# Connect to the Excel file on the local machine
excel_app = win32com.client.Dispatch("Excel.Application")
excel_app.Visible = False
workbook = excel_app.Workbooks.Open(excel_file)
# Get file information
file_info = {
'name': os.path.basename(excel_file),
'path': excel_file,
'size': os.path.getsize(excel_file),
'author': workbook.BuiltinDocumentProperties("Author").Value,
'owner': workbook.BuiltinDocumentProperties("Owner").Value,
'create_datetime': datetime.fromtimestamp(os.path.getctime(excel_file)),
'last_edited': datetime.fromtimestamp(os.path.getmtime(excel_file)),
'last_accessed': datetime.fromtimestamp(os.path.getatime(excel_file)),
'data_connections': [],
'sheets': []
}
# Get data connections
data_connections = workbook.Connections
for conn in data_connections:
file_info['data_connections'].append({
'name': conn.Name,
'source': conn.Source
})
# Get sheets information
for sheet in workbook.Worksheets:
sheet_info = {
'name': sheet.Name,
'row_count': sheet.UsedRange.Rows.Count,
'pivot_charts': 0
}
for chart in sheet.Shapes:
if chart.Chart.ChartType == 51:
sheet_info['pivot_charts'] += 1
file_info['sheets'].append(sheet_info)
excel_file_info.append(file_info)
# Close the workbook and quit the Excel application
workbook.Close()
excel_app.Quit()
except Exception as e:
print(f"Error processing {excel_file}: {e}")
return excel_file_info
# Example usage
excel_files_info = get_excel_file_info(r"C:\path\to\your\excel\files")
for file_info in excel_files_info:
print(file_info)
Please note that this script assumes that you have Excel installed on your local machine and that you have the necessary permissions to access the Excel files.
To run this script on a remote server, you can use the win32com.client library to connect to the remote server's Excel application. However, this requires that the remote server has Excel installed and that you have the necessary permissions to access the Excel files on the remote server.
Here's an example of how to connect to a remote server using the UNC path:
python
def get_excel_file_info_remote(unc_path):
excel_files = []
for root, dirs, files in os.walk(unc_path):
for file in files:
if file.lower().endswith('.xlsx'):
excel_files.append(os.path.join(root, file))
excel_file_info = []
for excel_file in excel_files:
try:
# Connect to the Excel file on the remote server
excel_app = win32com.client.Dispatch("Excel.Application")
excel_app.Visible = False
workbook = excel_app.Workbooks.Open(excel_file)
# Get file information (same as above)
# Close the workbook and quit the Excel application
workbook.Close()
excel_app.Quit()
excel_file_info.append(file_info)
except Exception as e:
print(f"Error processing {excel_file}: {e}")
return excel_file_info
# Example usage
excel_files_info_remote = get_excel_file_info_remote(r"\\remote\server\path\to\excel\files")
for file_info in excel_files_info_remote:
print(file_info)
Please replace \\remote\server\path\to\excel\files with the actual UNC path to the remote server's Excel files.