Spaces:
Sleeping
Sleeping
Download app.py from shen0625/Word-Formatter: direct link, hf CLI and curl.
- Browser
- Download file 3.23 kB
-
https://huggingface.co/spaces/shen0625/Word-Formatter/resolve/main/app.py
- Command line
-
hf download hf://spaces/shen0625/Word-Formatter/app.py
-
curl -L -o app.py https://huggingface.co/spaces/shen0625/Word-Formatter/resolve/main/app.py
3.23 kB
| import gradio as gr | |
| from docx import Document | |
| from docx.shared import Pt | |
| from docx.enum.text import WD_ALIGN_PARAGRAPH | |
| from docx.oxml.ns import qn | |
| def set_font_style(run, font_name, size, bold=False): | |
| run.font.name = font_name | |
| run._element.rPr.rFonts.set(qn('w:eastAsia'), font_name) | |
| run.font.size = Pt(size) | |
| run.font.bold = bold | |
| def format_word_document(input_file): | |
| if input_file is None: | |
| return None | |
| doc_path = input_file.name | |
| output_path = doc_path.replace(".docx", "_排版已完成.docx") | |
| doc = Document(doc_path) | |
| h1_idx = fig_idx = tab_idx = 0 | |
| style_config = { | |
| 'Heading 1': {'font': '宋体', 'size': 22, 'bold': True, 'align': WD_ALIGN_PARAGRAPH.RIGHT}, | |
| 'Heading 2': {'font': '宋体', 'size': 16, 'bold': True, 'align': WD_ALIGN_PARAGRAPH.LEFT}, | |
| 'Heading 3': {'font': '宋体', 'size': 14, 'bold': True, 'align': WD_ALIGN_PARAGRAPH.LEFT}, | |
| 'Heading 4': {'font': '宋体', 'size': 14, 'bold': True, 'align': WD_ALIGN_PARAGRAPH.LEFT}, | |
| 'Heading 6': {'font': '黑体', 'size': 14, 'bold': True, 'align': WD_ALIGN_PARAGRAPH.LEFT}, | |
| 'Heading 7': {'font': '黑体', 'size': 12, 'bold': True, 'align': WD_ALIGN_PARAGRAPH.LEFT}, | |
| 'Normal': {'font': '仿宋_GB2312', 'size': 12, 'bold': False, 'align': WD_ALIGN_PARAGRAPH.LEFT} | |
| } | |
| for para in doc.paragraphs: | |
| style_name = para.style.name | |
| if style_name == 'Heading 1': | |
| h1_idx += 1 | |
| fig_idx = tab_idx = 0 | |
| para.paragraph_format.page_break_before = True | |
| cfg = style_config['Heading 1'] | |
| elif style_name in style_config: | |
| cfg = style_config[style_name] | |
| else: | |
| cfg = style_config['Normal'] | |
| para.alignment = cfg['align'] | |
| for run in para.runs: | |
| set_font_style(run, cfg['font'], cfg['size'], cfg['bold']) | |
| if para.text.strip().startswith(('图', '表')): | |
| para.alignment = WD_ALIGN_PARAGRAPH.CENTER | |
| if para.text.strip().startswith('图'): | |
| fig_idx += 1 | |
| para.text = f"图 {h1_idx}-{fig_idx} {para.text.replace('图', '').strip()}" | |
| else: | |
| tab_idx += 1 | |
| para.text = f"表 {h1_idx}-{tab_idx} {para.text.replace('表', '').strip()}" | |
| for table in doc.tables: | |
| table.alignment = WD_ALIGN_PARAGRAPH.CENTER | |
| for i, row in enumerate(table.rows): | |
| for cell in row.cells: | |
| for p in cell.paragraphs: | |
| p.alignment = WD_ALIGN_PARAGRAPH.CENTER if i == 0 else WD_ALIGN_PARAGRAPH.LEFT | |
| for run in p.runs: | |
| set_font_style(run, '仿宋_GB2312', 12) | |
| doc.save(output_path) | |
| return output_path | |
| with gr.Blocks(title="一键排版") as demo: | |
| gr.Markdown("## 📄 Word 自动排版闭环工具") | |
| with gr.Row(): | |
| file_in = gr.File(label="1. 拖入你的文档 (.docx)") | |
| file_out = gr.File(label="2. 获取排版后文档", interactive=False) | |
| btn = gr.Button("🚀 立即处理", variant="primary") | |
| btn.click(fn=format_word_document, inputs=file_in, outputs=file_out) | |
| if __name__ == "__main__": | |
| demo.launch() |