|
| 1 | +import docx |
| 2 | +import pandas as pd |
| 3 | +from .excel import Excel |
| 4 | +import types |
| 5 | + |
| 6 | +def Word(filepath_or_buffer=None): |
| 7 | + """ |
| 8 | + Extends docx.document.Document |
| 9 | + """ |
| 10 | + # patch class |
| 11 | + docx.document.Document.extract_text = extract_text |
| 12 | + docx.document.Document.extract_tables = extract_tables |
| 13 | + docx.document.Document.tables_to_excel = tables_to_excel |
| 14 | + docx.document.Document.add_tables_from_excel = add_tables_from_excel |
| 15 | + docx.document.Document.add_picture = add_picture |
| 16 | + |
| 17 | + # make instance using patched class |
| 18 | + doc = docx.Document(filepath_or_buffer) |
| 19 | + |
| 20 | + return doc |
| 21 | + |
| 22 | +def extract_text(self): |
| 23 | + return '\n'.join([p.text for p in self.paragraphs]) |
| 24 | + |
| 25 | +def extract_tables(self): |
| 26 | + table_list = [] |
| 27 | + for table in self.tables: |
| 28 | + table_data = [] |
| 29 | + for row in table.rows: |
| 30 | + row_data = [] |
| 31 | + for cell in row.cells: |
| 32 | + cell_text = '' |
| 33 | + for p in cell.paragraphs: |
| 34 | + cell_text += p.text |
| 35 | + # if debug: |
| 36 | + # print(cell_text, end=' ') |
| 37 | + row_data.append(cell_text) |
| 38 | + # if debug: |
| 39 | + # print() |
| 40 | + table_data.append(row_data) |
| 41 | + |
| 42 | + try: |
| 43 | + import pandas as pd |
| 44 | + table_frame = pd.DataFrame(table_data) |
| 45 | + table_list.append(table_frame) |
| 46 | + except ImportError: |
| 47 | + # TODO: pandas가 없는 경우 텍스트로 출력 |
| 48 | + pass |
| 49 | + |
| 50 | + return table_list[0] if len(table_list) == 1 else table_list |
| 51 | + |
| 52 | +def tables_to_excel(self, filepath_or_buffer): |
| 53 | + table_list = self.extract_tables() |
| 54 | + |
| 55 | + excel_file = pd.ExcelWriter(filepath_or_buffer) |
| 56 | + for i, table_frame in enumerate(table_list): |
| 57 | + table_frame.to_excel(excel_file, 'Sheet{}'.format(i+1)) |
| 58 | + excel_file.save() |
| 59 | + |
| 60 | + return filepath_or_buffer |
| 61 | + |
| 62 | +def add_tables_from_excel(self, excel_filepath, sheetname=None, |
| 63 | + table_style='TableGrid'): |
| 64 | + table_frames = Excel(excel_filepath, sheetname=sheetname) |
| 65 | + |
| 66 | + if not isinstance(table_frames, list): |
| 67 | + table_frames = [table_frames] |
| 68 | + |
| 69 | + for frame in table_frames: |
| 70 | + frame = frame.fillna('') |
| 71 | + nrows, ncols = len(frame), len(frame.columns) |
| 72 | + table = self.add_table(rows=nrows, cols=ncols, style='TableGrid') |
| 73 | + # table.style = 'LightShading-Accent1' |
| 74 | + for i, row in enumerate(table.rows): |
| 75 | + for j, cell in enumerate(row.cells): |
| 76 | + cell.text = str(frame.iloc[i, j]) |
| 77 | + |
| 78 | +def add_picture(self, image_path_or_stream, width=None, height=None): |
| 79 | + if width: width = docx.shared.Inches(width) |
| 80 | + if height: height = docx.shared.Inches(height) |
| 81 | + |
| 82 | + run = self.add_paragraph().add_run() |
| 83 | + return run.add_picture(image_path_or_stream, width, height) |
0 commit comments