Question: Try to fix this code so it will have nice structure in texstudio / latex . import tkinter as tk # Import tkinter for GUI

Try to fix this code so it will have nice structure in texstudio/latex.
import tkinter as tk # Import tkinter for GUI
from tkinter import filedialog, messagebox # Import filedialog for file selection and messagebox for popups
from docx import Document # Import Document from the python-docx library to read Word files
def word_to_latex(doc_paths):
"""Converts multiple Word documents to LaTeX format."""
latex_content ="" # Initialize a string to hold the LaTeX content
# Loop through all selected Word documents
for doc_path in doc_paths:
doc = Document(doc_path) # Open the Word document
for paragraph in doc.paragraphs: # Loop through each paragraph in the document
text = paragraph.text.strip() # Get the text of the paragraph and remove leading/trailing spaces
if text: # If the paragraph is not empty
# Add space between "Authors:" and affiliation
if "Authors:" in text:
latex_content += f"{text}\\\\
" # Adds extra line space after "Authors:"
# Add space between "Affiliation" and "Abstract"
elif "Affiliation" in text and "Abstract" in text:
latex_content += f"{text}\\\\
" # Adds extra space between Affiliation and Abstract
else:
# Check for bold and italic formatting
bold = paragraph.style.font.bold
italic = paragraph.style.font.italic
if bold and italic:
text = f"\\textbf{{\\textit{{{text}}}}}" # Apply both bold and italic formatting
elif bold:
text = f"\\textbf{{{text}}}" # Apply bold formatting
elif italic:
text = f"\\textit{{{text}}}" # Apply italic formatting
# Handle section headings (e.g., Chapter, Section)
if paragraph.style.name.startswith('Heading'): # Check if it's a heading
level = int(paragraph.style.name.split('')[-1]) # Get the heading level (1,2, or 3)
if level ==1: # Level 1 heading (Section)
latex_content += f"\\section{{{text}}}
"
elif level ==2: # Level 2 heading (Subsection)
latex_content += f"\\subsection{{{text}}}
"
else: # Level 3 heading (Subsubsection)
latex_content += f"\\subsubsection{{{text}}}
"
else:
latex_content += f"{text}\\\\
" # Regular text with line break
latex_content +="\
ewpage
" # Add a page break between documents
# Add a centered placeholder text in large, bold font
centered_placeholder ="\\begin{center}\\textbf{\\Large Placeholder Text}\\end{center}
"
# Wrap the content in a basic LaTeX document structure (class, encoding, document start and end)
latex_document = f"""\\documentclass{{article}}
\\usepackage[utf8]{{inputenc}}
\\usepackage[T1]{{fontenc}}
\\begin{{document}}
{centered_placeholder} # Insert the centered placeholder text
{latex_content} # Insert the LaTeX content generated from the Word document
\\end{{document}}
"""
return latex_document # Return the LaTeX formatted document
def save_latex(latex_content):
"""Saves the LaTeX content to a file."""
save_path = filedialog.asksaveasfilename(defaultextension=".tex", # Prompt user to choose file path
filetypes=[("LaTeX files", "*.tex")]) # Specify file types
if save_path: # If the user selected a valid path
try:
with open(save_path, "w", encoding="utf-8") as f: # Open the file for writing
f.write(latex_content) # Write the LaTeX content to the file
messagebox.showinfo("Success", f"LaTeX file saved at: {save_path}") # Show success message
except Exception as e: # If an error occurs while saving
messagebox.showerror("Error", f"Could not save file: {e}") # Show error message
def open_files():
"""Opens multiple Word files and converts them to LaTeX."""
file_paths = filedialog.askopenfilenames(filetypes=[("Word files", "*.docx")]) # Let user select Word files
if file_paths: # If the user selected files
try:
latex_content = word_to_latex(file_paths) # Convert Word files to LaTeX content
save_latex(latex_content) # Save the generated LaTeX content to a file
except Exception as e: # If an error occurs during the conversion process
messagebox.showerror("Error", f"Could not convert files: {e}") # Show error message
# Set up the GUI (Graphical User Interface)
root = tk.Tk() # Create the main window for the G

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!