-
Notifications
You must be signed in to change notification settings - Fork 0
/
your_excel_file.py
62 lines (48 loc) · 1.94 KB
/
your_excel_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import openpyxl
#
#
#
#
# AQUESTA ÉS LA APP QUE EN UN MOMENT ENS VA SERVIR PER
# ACCEDIR A LES DADES DINS EL FITXER DADES_MUNICIPIS
# I EXTREURE EN FORMAT .TXT UN FITXER AMB LA COLUMNA
# SEL·LECCIONADA
#
#
# Open the Excel file
workbook = openpyxl.load_workbook('Dades_Municipis.xlsx')
# Get the names of all the sheets in the workbook
sheet_names = workbook.sheetnames
# Print the names of all the sheets
print("Available Sheets:")
for idx, name in enumerate(sheet_names, start=1):
print(f"{idx}. {name}")
# Ask the user to select a sheet
selected_sheet_idx = int(input("Enter the index of the sheet you want to access: "))
selected_sheet_name = sheet_names[selected_sheet_idx - 1] # Adjust for 0-based indexing
# Access the selected sheet
sheet = workbook[selected_sheet_name]
# Get the dimensions of the sheet (number of rows and columns)
max_row = sheet.max_row
max_column = sheet.max_column
# Print the names of all the columns in the selected sheet
print("Available Columns:")
for col in range(1, max_column + 1):
column_letter = openpyxl.utils.get_column_letter(col)
print(f"{col}. {column_letter}")
# Ask the user to select a column
selected_column_idx = int(input("Enter the index of the column you want to extract: "))
selected_column_letter = openpyxl.utils.get_column_letter(selected_column_idx)
# Extract the values from the selected column
column_values = []
for row in range(1, max_row + 1):
cell_value = sheet[selected_column_letter + str(row)].value
column_values.append(cell_value)
# Create a .txt file and write the extracted column values to it
output_file_path = f"{selected_sheet_name}_column_{selected_column_letter}.txt"
with open(output_file_path, 'w') as txt_file:
for value in column_values:
txt_file.write(str(value) + '\n')
print(f"Column values saved to '{output_file_path}'")
# Close the workbook when done
workbook.close()