Jan-19-2025, 09:31 AM
(Jan-19-2025, 09:02 AM)Keville_35 Wrote: [ -> ]This script creates a simple .docx file with some bold and italic text:It doesn't look like Python code. Which language is that? Javascript?
(Jan-19-2025, 09:02 AM)Keville_35 Wrote: [ -> ]This script creates a simple .docx file with some bold and italic text:It doesn't look like Python code. Which language is that? Javascript?
(Jan-19-2025, 09:02 AM)Keville_35 Wrote: [ -> ]This script creates a simple .docx file with some bold and italic text:
import { Document, Paragraph, TextRun } from "docx"; import { writeFileSync } from "fs"; // Create document const doc = new Document({ sections: [{ properties: {}, children: [ new Paragraph({ children: [ new TextRun({ text: "Hello World", bold: true, }), new TextRun({ text: " This is a new paragraph.", italics: true, }), ], }), ], }], }); // Used to export the file into a .docx file const buffer = await Packer.toBuffer(doc); // Write the file writeFileSync("My Document.docx", buffer); console.log("Document created successfully");
from docx import Document from docx.shared import Pt from docx.enum.text import WD_PARAGRAPH_ALIGNMENT # 创建一个文档对象 doc = Document() # 添加标题 doc.add_heading('Formatted Text Example', level=1) # 添加一个段落,并设置加粗、斜体和字体大小 paragraph = doc.add_paragraph() run = paragraph.add_run("This text is bold, italic, and larger.") run.bold = True run.italic = True run.font.size = Pt(16) # 添加另一个段落并改变字体 paragraph2 = doc.add_paragraph() run2 = paragraph2.add_run("This text uses a custom font and size.") run2.font.name = 'Arial' run2.font.size = Pt(14) # 居中对齐 paragraph2.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 保存文档 doc.save("formatted_text.docx") print("Word document created successfully!")
Quote:pip install python-docxyou must install python-docx.