from PyQt5.
QtWidgets import QDialog, QVBoxLayout, QCheckBox,
QPushButton, QFileDialog, QMessageBox
from docx import Document
from docx.shared import Inches
class ExportOptionsDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Export Options")
self.layout = QVBoxLayout()
self.summary_checkbox = QCheckBox("Include summary table of
codes/tags")
self.layout.addWidget(self.summary_checkbox)
self.export_button = QPushButton("Export")
self.export_button.clicked.connect(self.accept)
self.layout.addWidget(self.export_button)
self.setLayout(self.layout)
def include_summary(self):
return self.summary_checkbox.isChecked()
def export_to_word(parent, project_data, include_code_summary=False):
try:
file_path, _ = QFileDialog.getSaveFileName(parent, "Export to Word", "",
"Word Document (*.docx)")
if not file_path:
return
if not file_path.endswith(".docx"):
file_path += ".docx"
document = Document()
document.add_heading("Exported Project Data", level=1)
for item in project_data:
document.add_heading(item.get("title", "Untitled"), level=2)
document.add_paragraph(item.get("content", ""))
if item.get("tags"):
document.add_heading("Codes/Tags", level=3)
for tag in item["tags"]:
document.add_paragraph(f"- {tag}", style="List Bullet")
if include_code_summary:
all_tags = {}
for item in project_data:
for tag in item.get("tags", []):
all_tags[tag] = all_tags.get(tag, 0) + 1
document.add_page_break()
document.add_heading("Summary Table of Codes/Tags", level=2)
table = document.add_table(rows=1, cols=2)
table.style = 'Table Grid'
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Tag/Code'
hdr_cells[1].text = 'Frequency'
for tag, freq in sorted(all_tags.items()):
row_cells = table.add_row().cells
row_cells[0].text = tag
row_cells[1].text = str(freq)
document.save(file_path)
QMessageBox.information(parent, "Export Successful", f"Project
successfully exported to:\n{file_path}")
except Exception as e:
QMessageBox.critical(parent, "Export Failed", f"An error occurred during
export:\n{str(e)}")