-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcsv2word-pandas.py
More file actions
executable file
·91 lines (67 loc) · 3.03 KB
/
csv2word-pandas.py
File metadata and controls
executable file
·91 lines (67 loc) · 3.03 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
""" csv2word-pandas.py at https://github.com/wilsonmar/python-samples/blob/main/csv2word-pandas.py
STATUS: NOT WORKING. ModuleNotFoundError: No module named 'pandas' using Python 3.12.7
git commit -m "v002 + DELETE_AFTER_CREATION :csv2word-pandas.py"
This create from a CSV file a MS-Word .docx file containing a table with multiple columns.
The table in the Word document will automatically adjust its size to fit the data.
The CSV file can contain any number of columns or rows.
Based on code generated by Perplexity.ai on 24-11-12 from prompt.
Before running this:
source venv/bin/activate
python3 -m pip install pandas # To read and handle the CSV file.
python3 -m pip install python-docx # To create and manipulate Word documents.
python -c "import pandas" # Test that pandas is installed.
chmod +x csv2word-pandas.py
./csv2word-pandas.py
deactivate
"""
import pandas as pd
from docx import Document
# Globals
CSV_FILE = 'country_info.csv' # Replace with your CSV file path
WORD_FILE = 'data/country_info.docx' # The Word file you want to generate
DELETE_AFTER_CREATION = False
def csv_to_word(csv_file, word_file):
# Step 1: Read the CSV file using pandas:
df = pd.read_csv(csv_file)
# Step 2: Create a new Word document:
doc = Document()
# Add a title (optional)
doc.add_heading('CSV Data in Word Format', level=1)
# Step 3: Add a table to the Word document with the same number of columns as the CSV
# Create a table with the same number of rows and columns as the dataframe
table = doc.add_table(rows=1, cols=len(df.columns))
# Step 4: Add headers to the table (column names)
hdr_cells = table.rows[0].cells
for i, column_name in enumerate(df.columns):
hdr_cells[i].text = column_name
# Step 5: Populate the table with data from the CSV
for index, row in df.iterrows():
row_cells = table.add_row().cells
for i, value in enumerate(row):
row_cells[i].text = str(value)
# Step 6: Save the Word document
doc.save(word_file)
# Example usage:
if __name__ == "__main__":
csv_to_word(CSV_FILE, WORD_FILE)
print(f"*** Word file '{word_file}' has been created successfully.")
if DELETE_AFTER_CREATION:
os.remove(word_file)
print(f"*** Word file '{word_file}' was deleted.")
""" Explanation of the Code:
Example CSV (example.csv):
Name, Age, Occupation
John, 28, Engineer
Alice, 30, Data Scientist
Bob, 25, Designer
Read the CSV File:
1. The pandas.read_csv() function is used to load the CSV file into a DataFrame (df). This allows you to easily access and manipulate the data.
2. Create a Word Document:
3. A new Word document is created using Document() from the python-docx library.
4. Add a Table to the Word Document:
5. The first row is used to add the headers (i.e., column names from the CSV).
6. We then loop through the rows of the DataFrame (df.iterrows()) and add each row of data into the Word table.
7. Save the Word Document:
8. The document is saved with the specified file name using doc.save().
"""