-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhwp_mcp_stdio_server.py
1269 lines (1087 loc) · 51.8 KB
/
hwp_mcp_stdio_server.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import json
import traceback
import logging
import ssl
from threading import Thread
import time
# Configure logging
logging.basicConfig(
level=logging.INFO,
filename="hwp_mcp_stdio_server.log",
filemode="a",
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
# 추가 스트림 핸들러 설정 (별도로 추가)
stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
logger = logging.getLogger("hwp-mcp-stdio-server")
logger.addHandler(stderr_handler)
# Optional: Disable SSL certificate validation for development
ssl._create_default_https_context = ssl._create_unverified_context
# Set up paths
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_dir)
try:
# Import FastMCP library
from mcp.server.fastmcp import FastMCP
logger.info("FastMCP successfully imported")
except ImportError as e:
logger.error(f"Failed to import FastMCP: {str(e)}")
print(f"Error: Failed to import FastMCP. Please install with 'pip install mcp'", file=sys.stderr)
sys.exit(1)
# Try to import HwpController
try:
from src.tools.hwp_controller import HwpController
logger.info("HwpController imported successfully")
except ImportError as e:
logger.error(f"Failed to import HwpController: {str(e)}")
# Try alternate paths
try:
sys.path.append(os.path.join(current_dir, "src"))
sys.path.append(os.path.join(current_dir, "src", "tools"))
from hwp_controller import HwpController
logger.info("HwpController imported from alternate path")
except ImportError as e2:
logger.error(f"Could not find HwpController in any path: {str(e2)}")
print(f"Error: Could not find HwpController module", file=sys.stderr)
sys.exit(1)
# Try to import HwpTableTools
try:
from src.tools.hwp_table_tools import HwpTableTools
logger.info("HwpTableTools imported successfully")
except ImportError as e:
logger.error(f"Failed to import HwpTableTools: {str(e)}")
# Try alternate paths
try:
from hwp_table_tools import HwpTableTools
logger.info("HwpTableTools imported from alternate path")
except ImportError as e2:
logger.error(f"Could not find HwpTableTools in any path: {str(e2)}")
print(f"Error: Could not find HwpTableTools module", file=sys.stderr)
sys.exit(1)
# Initialize FastMCP server
mcp = FastMCP(
"hwp-mcp",
version="0.1.0",
description="HWP MCP Server for controlling Hangul Word Processor",
dependencies=["pywin32>=305"],
env_vars={}
)
# Global HWP controller instance
hwp_controller = None
# Global HWP table tools instance
hwp_table_tools = None
def get_hwp_controller():
"""Get or create HwpController instance."""
global hwp_controller, hwp_table_tools
if hwp_controller is None:
logger.info("Creating HwpController instance...")
try:
hwp_controller = HwpController()
if not hwp_controller.connect(visible=True):
logger.error("Failed to connect to HWP program")
return None
# 테이블 도구 인스턴스도 초기화
hwp_table_tools = HwpTableTools(hwp_controller)
logger.info("Successfully connected to HWP program")
except Exception as e:
logger.error(f"Error creating HwpController: {str(e)}", exc_info=True)
return None
return hwp_controller
def get_hwp_table_tools():
"""Get or create HwpTableTools instance."""
global hwp_table_tools, hwp_controller
if hwp_table_tools is None:
hwp_controller = get_hwp_controller()
if hwp_controller:
hwp_table_tools = HwpTableTools(hwp_controller)
return hwp_table_tools
@mcp.tool()
def hwp_create() -> str:
"""Create a new HWP document."""
try:
hwp = get_hwp_controller()
if not hwp:
return "Error: Failed to connect to HWP program"
if hwp.create_new_document():
logger.info("Successfully created new document")
return "New document created successfully"
else:
return "Error: Failed to create new document"
except Exception as e:
logger.error(f"Error creating document: {str(e)}", exc_info=True)
return f"Error: {str(e)}"
@mcp.tool()
def hwp_open(path: str) -> str:
"""Open an existing HWP document."""
try:
if not path:
return "Error: File path is required"
hwp = get_hwp_controller()
if not hwp:
return "Error: Failed to connect to HWP program"
if hwp.open_document(path):
logger.info(f"Successfully opened document: {path}")
return f"Document opened: {path}"
else:
return "Error: Failed to open document"
except Exception as e:
logger.error(f"Error opening document: {str(e)}", exc_info=True)
return f"Error: {str(e)}"
@mcp.tool()
def hwp_save(path: str = None) -> str:
"""Save the current HWP document."""
try:
hwp = get_hwp_controller()
if not hwp:
return "Error: Failed to connect to HWP program"
if path:
if hwp.save_document(path):
logger.info(f"Successfully saved document to: {path}")
return f"Document saved to: {path}"
else:
return "Error: Failed to save document"
else:
temp_path = os.path.join(os.getcwd(), "temp_document.hwp")
if hwp.save_document(temp_path):
logger.info(f"Successfully saved document to temporary location: {temp_path}")
return f"Document saved to: {temp_path}"
else:
return "Error: Failed to save document"
except Exception as e:
logger.error(f"Error saving document: {str(e)}", exc_info=True)
return f"Error: {str(e)}"
@mcp.tool()
def hwp_insert_text(text: str, preserve_linebreaks: bool = True) -> str:
"""Insert text at the current cursor position."""
try:
if not text:
return "Error: Text is required"
hwp = get_hwp_controller()
if not hwp:
return "Error: Failed to connect to HWP program"
# 현재 커서가 표 안에 있는지 확인
is_in_table = False
try:
hwp.hwp.Run("TableCellBlock")
hwp.hwp.Run("Cancel")
is_in_table = True
except:
is_in_table = False
# 줄바꿈 문자 처리
if preserve_linebreaks and ('\n' in text or '\\n' in text):
# 이스케이프된 줄바꿈 문자(\n)와 실제 줄바꿈 문자 모두 처리
processed_text = text.replace('\\n', '\n')
lines = processed_text.split('\n')
success = True
for i, line in enumerate(lines):
if not hwp.insert_text(line):
success = False
break
# 마지막 줄이 아니면 줄바꿈 삽입
if i < len(lines) - 1:
hwp.insert_paragraph()
if success:
logger.info("Successfully inserted text with line breaks")
return "Text with line breaks inserted successfully"
else:
return "Error: Failed to insert text with line breaks"
else:
if hwp.insert_text(text):
# 표 안이 아닐 경우에만 커서를 오른쪽으로 이동
if not is_in_table:
# 현재 위치 저장
current_pos = hwp.hwp.GetPos()
if current_pos:
# 텍스트 길이만큼 오른쪽으로 이동
for _ in range(len(text)):
hwp.hwp.Run("CharRight")
logger.info("Successfully inserted text")
return "Text inserted successfully"
else:
return "Error: Failed to insert text"
except Exception as e:
logger.error(f"Error inserting text: {str(e)}", exc_info=True)
return f"Error: {str(e)}"
@mcp.tool()
def hwp_set_font(
name: str = None,
size: int = None,
bold: bool = False,
italic: bool = False,
underline: bool = False,
select_previous_text: bool = False
) -> str:
"""Set font properties for selected text."""
try:
hwp = get_hwp_controller()
if not hwp:
return "Error: Failed to connect to HWP program"
# 현재 선택된 텍스트에 대해 글자 모양 설정
if hwp.set_font_style(
font_name=name,
font_size=size,
bold=bold,
italic=italic,
underline=underline,
select_previous_text=select_previous_text
):
logger.info("Successfully set font")
return "Font set successfully"
else:
return "Error: Failed to set font"
except Exception as e:
logger.error(f"Error setting font: {str(e)}", exc_info=True)
return f"Error: {str(e)}"
@mcp.tool()
def hwp_insert_table(rows: int, cols: int) -> str:
"""Insert a table at the current cursor position."""
try:
# HwpTableTools 인스턴스 가져오기
table_tools = get_hwp_table_tools()
if not table_tools:
return "Error: Failed to get table tools instance"
return table_tools.insert_table(rows, cols)
except Exception as e:
logger.error(f"Error inserting table: {str(e)}", exc_info=True)
return f"Error: {str(e)}"
@mcp.tool()
def hwp_insert_paragraph() -> str:
"""Insert a new paragraph."""
try:
hwp = get_hwp_controller()
if not hwp:
return "Error: Failed to connect to HWP program"
if hwp.insert_paragraph():
logger.info("Successfully inserted paragraph")
return "Paragraph inserted successfully"
else:
return "Error: Failed to insert paragraph"
except Exception as e:
logger.error(f"Error inserting paragraph: {str(e)}", exc_info=True)
return f"Error: {str(e)}"
@mcp.tool()
def hwp_get_text() -> str:
"""Get the text content of the current document."""
try:
hwp = get_hwp_controller()
if not hwp:
return "Error: Failed to connect to HWP program"
text = hwp.get_text()
if text is not None:
logger.info("Successfully retrieved document text")
return text
else:
return "Error: Failed to get document text"
except Exception as e:
logger.error(f"Error getting text: {str(e)}", exc_info=True)
return f"Error: {str(e)}"
@mcp.tool()
def hwp_close(save: bool = True) -> str:
"""Close the HWP document and connection."""
try:
global hwp_controller, hwp_table_tools
if hwp_controller and hwp_controller.is_hwp_running:
if hwp_controller.disconnect():
logger.info("Successfully closed HWP connection")
hwp_controller = None
hwp_table_tools = None
return "HWP connection closed successfully"
else:
return "Error: Failed to close HWP connection"
else:
return "HWP is already closed"
except Exception as e:
logger.error(f"Error closing HWP: {str(e)}", exc_info=True)
return f"Error: {str(e)}"
@mcp.tool()
def hwp_ping_pong(message: str = "핑") -> str:
"""
핑퐁 테스트용 함수입니다. 핑을 보내면 퐁을 응답하고, 퐁을 보내면 핑을 응답합니다.
Args:
message: 테스트 메시지 (기본값: "핑")
Returns:
str: 응답 메시지
"""
try:
logger.info(f"핑퐁 테스트 함수 호출됨: 메시지 - {message}")
# 메시지에 따라 응답 생성
if message == "핑":
response = "퐁"
elif message == "퐁":
response = "핑"
else:
response = f"모르는 메시지입니다: {message} (핑 또는 퐁을 보내주세요)"
# 응답 생성 시간 기록
current_time = time.strftime("%Y-%m-%d %H:%M:%S")
# 응답 데이터 구성
result = {
"response": response,
"original_message": message,
"timestamp": current_time
}
return json.dumps(result, ensure_ascii=False)
except Exception as e:
logger.error(f"핑퐁 테스트 함수 오류: {str(e)}", exc_info=True)
return f"테스트 오류 발생: {str(e)}"
@mcp.tool()
def hwp_create_table_with_data(rows: int, cols: int, data = None, has_header: bool = False) -> str:
"""
pywin32를 사용하여 현재 커서 위치에 표를 생성하고 데이터를 채웁니다.
Args:
rows: 표의 행 수
cols: 표의 열 수
data: 표에 채울 데이터 (JSON 문자열 또는 파이썬 리스트)
has_header: 첫 번째 행을 헤더로 처리할지 여부
Returns:
str: 결과 메시지
"""
try:
# HwpTableTools 인스턴스 가져오기
table_tools = get_hwp_table_tools()
if not table_tools:
return "Error: Failed to get table tools instance"
# 현재 커서가 표 안에 있는지 확인
hwp = get_hwp_controller()
is_in_table = False
try:
hwp.hwp.Run("TableCellBlock")
hwp.hwp.Run("Cancel")
is_in_table = True
except:
is_in_table = False
# 표 안에 있지 않은 경우에만 새 표 생성
if not is_in_table:
# 표 생성
if not table_tools.insert_table(rows, cols):
return "Error: Failed to create table"
# 데이터가 있는 경우 표 채우기
if data is not None:
# 데이터 형식 로깅
logger.info(f"Create table with data type: {type(data)}, data: {str(data)[:100]}...")
# 데이터가 이미 리스트 형태인 경우
if isinstance(data, list):
logger.info("Data is already a list, using directly")
processed_data = data
# 데이터가 문자열인 경우 JSON 파싱 시도
elif isinstance(data, str):
try:
import json
try:
processed_data = json.loads(data)
logger.info(f"Successfully parsed JSON data with {len(processed_data)} rows")
except json.JSONDecodeError as e:
logger.error(f"JSON 파싱 오류: {str(e)}")
try:
import ast
processed_data = ast.literal_eval(data)
logger.info(f"Successfully parsed data with literal_eval")
except Exception as e2:
logger.error(f"리터럴 평가 오류: {str(e2)}")
return f"표는 생성되었으나 JSON 데이터 파싱 오류: {str(e)}"
except Exception as e:
logger.error(f"데이터 파싱 오류: {str(e)}", exc_info=True)
return f"표는 생성되었으나 데이터 파싱 오류: {str(e)}"
else:
return f"표는 생성되었으나 지원되지 않는 데이터 유형: {type(data)}"
# 데이터 구조 유효성 검사
if not isinstance(processed_data, list):
return f"표는 생성되었으나 데이터가 리스트 형식이 아닙니다: {type(processed_data)}"
if len(processed_data) == 0:
return "표는 생성되었으나 데이터 리스트가 비어 있습니다."
# 모든 행이 리스트인지 확인 및 변환
for i, row in enumerate(processed_data):
if not isinstance(row, list):
logger.warning(f"Row {i} is not a list, converting: {row}")
processed_data[i] = [row]
# 모든 데이터를 문자열로 변환
string_data = []
for row in processed_data:
string_row = [str(cell) if cell is not None else "" for cell in row]
string_data.append(string_row)
# 표에 데이터 채우기
if table_tools.fill_table_with_data(string_data, 1, 1, has_header):
return f"표 생성 및 데이터 입력 완료 ({rows}x{cols})"
else:
return "표는 생성되었으나 데이터 입력에 실패했습니다."
return f"표 생성 완료 ({rows}x{cols})"
except Exception as e:
logger.error(f"표 생성 중 오류: {str(e)}", exc_info=True)
return f"Error: {str(e)}"
@mcp.tool()
def hwp_create_complete_document(document_spec: dict) -> dict:
"""
전체 문서를 한 번의 호출로 작성합니다. 문서 구조, 내용 및 서식을 JSON으로 정의하여 전달합니다.
Args:
document_spec (dict): 문서 사양을 담은 딕셔너리. 다음과 같은 구조를 가집니다:
{
"title": "문서 제목", # 선택 사항
"filename": "저장할_파일명.hwp", # 선택 사항, 저장할 경우
"elements": [ # 필수: 문서를 구성하는 요소 배열
{
"type": "heading", # 요소 유형 (heading, text, paragraph, table, etc.)
"content": "제목", # 요소 내용
"properties": { # 요소 속성 (선택 사항)
"font_size": 16,
"bold": true,
...
}
},
...
],
"special_type": { # 특수 문서 유형 (선택 사항)
"type": "report", # 보고서 등 특수 문서 유형
"params": { ... } # 특수 문서에 필요한 매개변수
},
"save": true # 저장 여부 (선택 사항)
}
Returns:
dict: 문서 생성 결과
"""
try:
hwp = get_hwp_controller()
if not hwp:
return {"status": "error", "message": "Failed to connect to HWP program"}
# 새 문서 생성
if not hwp.create_new_document():
return {"status": "error", "message": "Failed to create new document"}
# 문서 사양 유효성 검사
if not document_spec:
return {"status": "error", "message": "Document specification is required"}
if "special_type" in document_spec:
# 특수 문서 유형 처리 (보고서 등)
special_type = document_spec["special_type"]
special_type_name = special_type.get("type", "")
special_params = special_type.get("params", {})
# 보고서 처리
if special_type_name == "report":
return _create_report(hwp, special_params, document_spec)
# 편지 처리
elif special_type_name == "letter":
return _create_letter(hwp, special_params, document_spec)
else:
return {"status": "error", "message": f"Unknown special document type: {special_type_name}"}
# 일반 문서 처리
elif "elements" in document_spec:
elements = document_spec.get("elements", [])
# 문서 요소 처리
for element in elements:
element_type = element.get("type", "")
content = element.get("content", "")
properties = element.get("properties", {})
# 요소 유형에 따른 처리
if element_type == "heading":
# 제목 스타일 설정
font_size = properties.get("font_size", 16)
bold = properties.get("bold", True)
hwp.set_font(None, font_size, bold, False)
hwp.insert_text(content)
hwp.insert_paragraph()
elif element_type == "text":
# 텍스트 스타일 설정
font_size = properties.get("font_size", 10)
bold = properties.get("bold", False)
italic = properties.get("italic", False)
hwp.set_font(None, font_size, bold, italic)
hwp.insert_text(content)
elif element_type == "paragraph":
hwp.insert_paragraph()
elif element_type == "table":
rows = properties.get("rows", 0)
cols = properties.get("cols", 0)
data = properties.get("data", [])
if rows > 0 and cols > 0:
hwp.insert_table(rows, cols)
# 테이블 데이터 채우기 (구현 필요)
# 현재는 표만 생성하고 데이터는 처리하지 않음
else:
logger.warning(f"Unknown element type: {element_type}")
else:
return {"status": "error", "message": "Document must contain 'elements' or 'special_type'"}
# 문서 저장
if document_spec.get("save", False):
filename = document_spec.get("filename", "generated_document.hwp")
if hwp.save_document(filename):
return {
"status": "success",
"message": "Document created and saved successfully",
"saved_path": filename
}
else:
return {
"status": "partial_success",
"message": "Document created but failed to save"
}
return {"status": "success", "message": "Document created successfully"}
except Exception as e:
logger.error(f"Error creating document: {str(e)}", exc_info=True)
return {"status": "error", "message": f"Error: {str(e)}"}
def _create_report(hwp, params, document_spec):
"""보고서 문서를 생성합니다."""
try:
title = params.get("title", "보고서 제목")
author = params.get("author", "작성자")
date = params.get("date", time.strftime("%Y년 %m월 %d일"))
sections = params.get("sections", [{"title": "섹션 제목", "content": "섹션 내용"}])
# 제목 페이지
hwp.set_font(None, 22, True, False)
hwp.insert_text(title)
hwp.insert_paragraph()
hwp.insert_paragraph()
hwp.set_font(None, 14, False, False)
hwp.insert_text(f"작성자: {author}")
hwp.insert_paragraph()
hwp.insert_text(f"작성일: {date}")
hwp.insert_paragraph()
hwp.insert_paragraph()
# 각 섹션
for section in sections:
section_title = section.get("title", "")
section_content = section.get("content", "")
hwp.set_font(None, 16, True, False)
hwp.insert_text(section_title)
hwp.insert_paragraph()
hwp.set_font(None, 12, False, False)
hwp.insert_text(section_content)
hwp.insert_paragraph()
hwp.insert_paragraph()
# 문서 저장
result = {"status": "success", "message": "Report created successfully"}
if document_spec.get("save", False):
filename = document_spec.get("filename", "report.hwp")
if hwp.save_document(filename):
result["saved_path"] = filename
else:
result["message"] = "Report created but failed to save"
result["status"] = "partial_success"
return result
except Exception as e:
logger.error(f"Error creating report: {str(e)}", exc_info=True)
return {"status": "error", "message": f"Error: {str(e)}"}
def _create_letter(hwp, params, document_spec):
"""편지 문서를 생성합니다."""
try:
title = params.get("title", "제목 없음")
recipient = params.get("recipient", "받는 사람")
content = params.get("content", "내용을 입력하세요.")
sender = params.get("sender", "보내는 사람")
date = params.get("date", time.strftime("%Y년 %m월 %d일"))
# 제목 (굵게, 크게)
hwp.set_font(None, 16, True, False)
hwp.insert_text(title)
hwp.insert_paragraph()
hwp.insert_paragraph()
# 받는 사람
hwp.set_font(None, 12, False, False)
hwp.insert_text(f"받는 사람: {recipient}")
hwp.insert_paragraph()
hwp.insert_paragraph()
# 내용
hwp.set_font(None, 12, False, False)
hwp.insert_text(content)
hwp.insert_paragraph()
hwp.insert_paragraph()
# 날짜 (오른쪽 정렬)
# 오른쪽 정렬은 현재 구현되어 있지 않으므로 공백으로 대체
hwp.set_font(None, 12, False, False)
hwp.insert_text("".ljust(40) + date)
hwp.insert_paragraph()
# 보내는 사람 (오른쪽 정렬, 굵게)
hwp.set_font(None, 12, True, False)
hwp.insert_text("".ljust(40) + sender)
# 문서 저장
result = {"status": "success", "message": "Letter created successfully"}
if document_spec.get("save", False):
filename = document_spec.get("filename", "letter.hwp")
if hwp.save_document(filename):
result["saved_path"] = filename
else:
result["message"] = "Letter created but failed to save"
result["status"] = "partial_success"
return result
except Exception as e:
logger.error(f"Error creating letter: {str(e)}", exc_info=True)
return {"status": "error", "message": f"Error: {str(e)}"}
@mcp.tool()
def hwp_create_document_from_text(content: str, title: str = None, format_content: bool = True, save_filename: str = None, preserve_linebreaks: bool = True) -> dict:
"""
단일 문자열로 된 텍스트 내용으로 문서를 생성합니다.
Args:
content (str): 문서 내용 (형식을 자동으로 감지하고 처리)
title (str, optional): 문서 제목. 없으면 첫 줄을 제목으로 사용.
format_content (bool): 내용 자동 포맷팅 여부 (줄바꿈, 문단 구분 등)
save_filename (str, optional): 저장할 파일 이름. 제공되지 않으면 저장하지 않음.
preserve_linebreaks (bool): 줄바꿈 유지 여부. True이면 원본 텍스트의 모든 줄바꿈 유지.
Returns:
dict: 문서 생성 결과
"""
try:
hwp = get_hwp_controller()
if not hwp:
return {"status": "error", "message": "Failed to connect to HWP program"}
# 새 문서 생성
if not hwp.create_new_document():
return {"status": "error", "message": "Failed to create new document"}
# 내용이 없는 경우
if not content:
return {"status": "error", "message": "Document content is required"}
# 내용을 줄로 분리
lines = content.split('\n')
# 빈 줄을 기준으로 블록 구분
blocks = []
current_block = []
for line in lines:
if line.strip(): # 빈 줄이 아닌 경우
current_block.append(line)
else: # 빈 줄인 경우 블록 구분
if current_block:
blocks.append(current_block)
current_block = []
# 마지막 블록 추가
if current_block:
blocks.append(current_block)
# 제목 처리
if not title and blocks:
# 첫 번째 블록의 첫 번째 줄을 제목으로 사용
title = blocks[0][0]
if len(blocks[0]) > 1:
blocks[0] = blocks[0][1:] # 첫 번째 줄 제거
else:
blocks = blocks[1:] # 첫 번째 블록 제거
# 제목 추가
if title:
# 먼저 폰트 설정 후 텍스트 입력 (수정된 방식)
hwp.set_font(None, 16, True, False)
hwp.insert_text(title)
hwp.insert_paragraph()
hwp.insert_paragraph()
# 내용 자동 포맷팅
if format_content:
# 블록 단위로 처리
for block in blocks:
# 블록 내 첫 번째 줄로 블록 유형 판단
first_line = block[0].strip() if block else ""
# 제목 형식 감지 (예: #으로 시작하면 제목)
if first_line.startswith('#'):
level = 0
for char in first_line:
if char == '#':
level += 1
else:
break
heading_text = first_line[level:].strip()
font_size = max(11, 16 - (level - 1)) # 제목 레벨에 따라 글자 크기 조정
# 먼저 폰트 설정 후 텍스트 입력 (수정된 방식)
hwp.set_font(None, font_size, True, False)
hwp.insert_text(heading_text)
hwp.insert_paragraph()
# 제목 이후의 줄들 처리 (있을 경우)
if len(block) > 1:
hwp.set_font(None, 11, False, False)
for line in block[1:]:
hwp.insert_text(line)
hwp.insert_paragraph()
# 글머리 기호 감지 (예: - 또는 * 으로 시작하면 글머리 기호)
elif first_line.startswith(('-', '*', '•')):
hwp.set_font(None, 11, False, False)
for line in block:
line_stripped = line.strip()
if line_stripped.startswith(('-', '*', '•')):
content_text = line_stripped[1:].strip()
hwp.insert_text(f"• {content_text}")
else:
hwp.insert_text(line_stripped)
hwp.insert_paragraph()
# 시 또는 줄바꿈이 중요한 텍스트 (각 줄을 개별적으로 처리)
elif preserve_linebreaks:
hwp.set_font(None, 11, False, False)
for line in block:
hwp.insert_text(line)
hwp.insert_paragraph()
# 일반 텍스트 (블록 전체를 하나의 단락으로 처리)
else:
hwp.set_font(None, 11, False, False)
block_text = '\n'.join(block)
hwp.insert_text(block_text)
hwp.insert_paragraph()
# 블록 사이에 추가 줄바꿈
hwp.insert_paragraph()
# 자동 포맷팅 없이 그대로 삽입 (줄바꿈 보존)
else:
hwp.set_font(None, 11, False, False)
for line in lines:
if line.strip(): # 내용이 있는 줄
hwp.insert_text(line)
hwp.insert_paragraph() # 빈 줄이든 내용이 있는 줄이든 항상 줄바꿈
# 문서 저장
result = {"status": "success", "message": "Document created from text successfully"}
if save_filename:
if hwp.save_document(save_filename):
result["saved_path"] = save_filename
else:
result["message"] = "Document created but failed to save"
result["status"] = "partial_success"
return result
except Exception as e:
logger.error(f"Error creating document from text: {str(e)}", exc_info=True)
return {"status": "error", "message": f"Error: {str(e)}"}
@mcp.tool()
def hwp_batch_operations(operations: list) -> dict:
"""
여러 HWP 작업을 한 번의 호출로 일괄 처리합니다.
Args:
operations (list): 실행할 작업 목록. 각 작업은 다음 형식의 딕셔너리입니다:
{
"operation": "작업명", # 예: "create", "set_font", "insert_text" 등
"params": {파라미터 딕셔너리} # 해당 작업에 필요한 파라미터
}
Returns:
dict: 각 작업의 실행 결과
"""
try:
hwp = get_hwp_controller()
if not hwp:
return {"status": "error", "message": "Failed to connect to HWP program"}
results = []
for op in operations:
operation = op.get("operation", "")
params = op.get("params", {})
result = {"operation": operation, "status": "success", "message": ""}
try:
if operation == "create":
if hwp.create_new_document():
result["message"] = "New document created successfully"
else:
result["status"] = "error"
result["message"] = "Failed to create new document"
elif operation == "open":
path = params.get("path", "")
if not path:
result["status"] = "error"
result["message"] = "File path is required"
elif hwp.open_document(path):
result["message"] = f"Document opened: {path}"
else:
result["status"] = "error"
result["message"] = "Failed to open document"
elif operation == "save":
path = params.get("path", None)
if path and hwp.save_document(path):
result["message"] = f"Document saved to: {path}"
elif not path:
temp_path = os.path.join(os.getcwd(), "temp_document.hwp")
if hwp.save_document(temp_path):
result["message"] = f"Document saved to: {temp_path}"
result["path"] = temp_path
else:
result["status"] = "error"
result["message"] = "Failed to save document"
else:
result["status"] = "error"
result["message"] = "Failed to save document"
elif operation == "insert_text":
text = params.get("text", "")
preserve_linebreaks = params.get("preserve_linebreaks", True)
if not text:
result["status"] = "error"
result["message"] = "Text is required"
elif preserve_linebreaks and ('\n' in text or '\\n' in text):
# 줄바꿈 보존 처리 개선
# 이스케이프된 줄바꿈 문자(\n)와 실제 줄바꿈 문자 모두 처리
# 먼저 이스케이프된 줄바꿈 문자를 실제 줄바꿈으로 변환
processed_text = text.replace('\\n', '\n')
lines = processed_text.split('\n')
success = True
for i, line in enumerate(lines):
if not hwp.insert_text(line):
success = False
break
# 마지막 줄이 아니면 줄바꿈 삽입
if i < len(lines) - 1:
hwp.insert_paragraph()
if success:
result["message"] = "Text with line breaks inserted successfully"
else:
result["status"] = "error"
result["message"] = "Failed to insert text with line breaks"
elif hwp.insert_text(text):
result["message"] = "Text inserted successfully"
else:
result["status"] = "error"
result["message"] = "Failed to insert text"
elif operation == "set_font":
name = params.get("name", None)
size = params.get("size", None)
bold = params.get("bold", False)
italic = params.get("italic", False)
underline = params.get("underline", False)
select_previous_text = params.get("select_previous_text", False)
if hwp.set_font_style(font_name=name, font_size=size, bold=bold, italic=italic, underline=underline, select_previous_text=select_previous_text):
result["message"] = "Font set successfully"
else:
result["status"] = "error"
result["message"] = "Failed to set font"
elif operation == "insert_paragraph":
count = params.get("count", 1) # 여러 줄 삽입 가능
success = True
for _ in range(count):
if not hwp.insert_paragraph():
success = False
break
if success:
result["message"] = f"{count} paragraph(s) inserted successfully"
else:
result["status"] = "error"
result["message"] = "Failed to insert paragraph"
elif operation == "insert_table":
rows = params.get("rows", 0)
cols = params.get("cols", 0)
data = params.get("data", [])
has_header = params.get("has_header", False)
table_tools = get_hwp_table_tools()
if not table_tools:
result["status"] = "error"
result["message"] = "Failed to get table tools instance"
elif rows <= 0 or cols <= 0:
result["status"] = "error"
result["message"] = "Valid rows and cols are required"
else:
# 데이터가 있으면 테이블 생성 후 데이터 채우기
if data:
resp = table_tools.create_table_with_data(rows, cols, json.dumps(data) if isinstance(data, list) else data, has_header)
result["message"] = resp
if resp.startswith("Error"):
result["status"] = "error"
else:
resp = table_tools.insert_table(rows, cols)