import json
import os
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.table import WD_TABLE_ALIGNMENT
def set_margins(section😞
# Set page margins to Narrow (0.5 inches)
section.left_margin = Pt(36) # 0.5 inch
section.right_margin = Pt(36) # 0.5 inch
section.top_margin = Pt(36) # 0.5 inch
section.bottom_margin = Pt(36) # 0.5 inch
def add_tables_to_word(data, output_file😞
document = Document()
# Set page margins
section = document.sections[0] # Get the first section
set_margins(section)
try:
# Add LNB headings
for key, value in data.items():
if 'LNB_heading' in value:
lnb_headings = value['LNB_heading']
for heading in lnb_headings:
p = document.add_paragraph(heading)
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
p.style.font.size = Pt(14)
p.style.font.bold = True # Assuming headings are bold as per usual document formatting
break # Only add headings from the first section
# Add tables
for section_name, section_data in data.items():
if 'LNB_heading' in section_data:
continue # Skip LNB heading section for tables
document.add_paragraph(section_name, style='Heading 1')
document.add_paragraph()
for table_name, table_info in section_data.items():
if table_name == 'Exp_info':
# Handle Exp_info table separately for stretching
headers = table_info['headers']
rows = table_info['rows']
# Add table name
document.add_paragraph(table_name, style='Heading 2')
document.add_paragraph()
# Create table with correct number of columns
num_cols = len(headers)
table = document.add_table(rows=1, cols=num_cols)
table.style = 'Table Grid'
table.alignment = WD_TABLE_ALIGNMENT.CENTER
# Add headers and calculate column widths based on header length
hdr_cells = table.rows[0].cells
for idx, header in enumerate(headers):
hdr_cells[idx].text = header
hdr_cells[idx].paragraphs[0].runs[0].font.size = Pt(10) # Set font size to 10pt
hdr_cells[idx].paragraphs[0].runs[0].font.bold = False # Ensure headers are not bold
# Calculate width based on header length
max_header_width = max(len(header), len(hdr_cells[idx].text))
table.columns[idx].width = Pt(max_header_width * 12) # Adjust multiplier as needed
# Add rows
for row_data in rows:
if isinstance(row_data, dict😞
# If row_data is a dictionary, extract values in order of headers
if any(row_data.values()): # Check if any value in row_data is non-empty
row_cells = table.add_row().cells
for idx, header in enumerate(headers):
cell_value = str(row_data.get(header, ''))
row_cells[idx].text = cell_value
row_cells[idx].paragraphs[0].runs[0].font.bold = False # Ensure cell text is not bold
row_cells[idx].paragraphs[0].runs[0].font.size = Pt(10) # Set font size to 10pt
elif isinstance(row_data, list😞
# If row_data is a list, assume it directly represents row values
if any(row_data): # Check if any value in row_data is non-empty
row_cells = table.add_row().cells
for idx, value in enumerate(row_data):
cell_value = str(value)
row_cells[idx].text = cell_value
row_cells[idx].paragraphs[0].runs[0].font.bold = False # Ensure cell text is not bold
row_cells[idx].paragraphs[0].runs[0].font.size = Pt(10) # Set font size to 10pt
else:
# Handle unexpected data types gracefully
print(f"Unexpected data type encountered in '{table_name}'")
# Add paragraph to separate tables
document.add_paragraph()
else:
# For other tables, add them as usual
headers = table_info['headers']
rows = table_info['rows']
# Add table name
document.add_paragraph(table_name, style='Heading 2')
document.add_paragraph()
# Create table with correct number of columns
num_cols = len(headers)
table = document.add_table(rows=1, cols=num_cols)
table.style = 'Table Grid'
table.alignment = WD_TABLE_ALIGNMENT.CENTER
# Add headers
hdr_cells = table.rows[0].cells
for idx, header in enumerate(headers):
hdr_cells[idx].text = header
hdr_cells[idx].paragraphs[0].runs[0].font.size = Pt(10) # Set font size to 10pt
hdr_cells[idx].paragraphs[0].runs[0].font.bold = False # Ensure headers are not bold
# Add rows
for row_data in rows:
if isinstance(row_data, dict😞
# If row_data is a dictionary, extract values in order of headers
if any(row_data.values()): # Check if any value in row_data is non-empty
row_cells = table.add_row().cells
for idx, header in enumerate(headers):
cell_value = str(row_data.get(header, ''))
row_cells[idx].text = cell_value
row_cells[idx].paragraphs[0].runs[0].font.bold = False # Ensure cell text is not bold
row_cells[idx].paragraphs[0].runs[0].font.size = Pt(10) # Set font size to 10pt
elif isinstance(row_data, list😞
# If row_data is a list, assume it directly represents row values
if any(row_data): # Check if any value in row_data is non-empty
row_cells = table.add_row().cells
for idx, value in enumerate(row_data):
cell_value = str(value)
row_cells[idx].text = cell_value
row_cells[idx].paragraphs[0].runs[0].font.bold = False # Ensure cell text is not bold
row_cells[idx].paragraphs[0].runs[0].font.size = Pt(10) # Set font size to 10pt
else:
# Handle unexpected data types gracefully
print(f"Unexpected data type encountered in '{table_name}'")
# Add paragraph to separate tables
document.add_paragraph()
# Save the single document in the specified output directory
document.save(output_file)
print(f"All tables and LNB headings added to the Word document '{output_file}' successfully.")
except Exception as e:
print(f"Error occurred while processing data: {e}")
def process_json_files(input_directory, output_directory😞
# Ensure the output directory exists
if not os.path.exists(output_directory):
os.makedirs(output_directory)
# Iterate over each file in the input directory
for filename in os.listdir(input_directory):
if filename.endswith(".json"😞
json_file_path = os.path.join(input_directory, filename)
json_filename = filename.split('.')[0]
output_file = os.path.join(output_directory, f"{json_filename}_output.docx")
# Load JSON data from each file
with open(json_file_path, 'r') as json_file:
data = json.load(json_file)
# Add tables and LNB headings to a single Word document for each JSON file
add_tables_to_word(data, output_file)
# Example usage
input_directory = r"C:\Users\sanket.shinde\Documents\JsonExtraction\JsonFiles"
output_directory = r"C:\Users\sanket.shinde\Documents\JsonExtraction\JsonData\Word_Files"
# Process all JSON files in the input directory
process_json_files(input_directory, output_directory)
I want to run this script in power automate desktop using Run Python script action.