Untitled

mail@pastecode.io avatar
unknown
plain_text
6 days ago
898 B
2
Indexable
Never
import openpyxl

# Read the input strings from a file
input_file = "D:\\Pro\\Cpp\\CollectWords\\input.txt"
output_file = "D:\\Pro\\Cpp\\CollectWords\\output.xlsx"

with open(input_file, "r", encoding='utf-8') as f:
    input_strings = f.readlines()

# Create a new Excel workbook and worksheet
wb = openpyxl.Workbook()
ws = wb.active

# Process each input string separately
for row_index, input_string in enumerate(input_strings, start=1):
    # Split the string into individual cells
    cells = input_string.split("|")

    # Remove empty cells and strip whitespace
    cells = [cell.strip() for cell in cells if cell.strip()]

    # Write the cells to the worksheet
    for column_index, cell in enumerate(cells, start=1):
        ws.cell(row=row_index, column=column_index).value = cell

# Save the workbook
wb.save(output_file)

print(f'Dữ liệu đã được ghi vào file {output_file}')
Leave a Comment