โค้ด: เลือกทั้งหมด
from docx import Document # Library ของ docx ที่ใช้
from docx.shared import Inches, Cm # Library ของ docx เป็นหน่วยนิ้ว และเซนติเมตร
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH # Library ของ docx รูปแบบของ paragraph
from docx.enum.section import WD_ORIENT # Library ของ docx รูปแบบของหน้ากระดาษ
document = Document()
name = "Yourname Lastname" # สร้างชื่อตัวแปรในรูปแบบ String
scale = "5 cm x 10 cm" # สร้างชื่อตัวแปรในรูปแบบ String
sections = document.sections
sections.orientation = WD_ORIENT.LANDSCAPE # ตั้งค่าหน้ากระดาษให้เป็นแนวนอน
for section in sections: # ตั้งค่าให้เป็นรูปแบบ 5ซม x10 ซม
section.page_height = Cm(5) # ความสูง
section.page_width = Cm(10) # ความกว้าง
section.top_margin = Cm(0.2) # ระยะห่างขอบบน
section.bottom_margin = Cm(0) # ระยะห่างขอบล่าง
section.left_margin = Cm(0.5) # ระยะห่างขอบซ้าย
section.right_margin = Cm(0.5) # ระยะห่างขอบขวา
print ("create WORD.....")
FIRSTLINE = document.add_paragraph(name) # โค้ดส่วนที่ 1 ของการเพิ่มข้อมูลลงใน word
paragraph_format = FIRSTLINE.paragraph_format
FIRSTLINE.style = 'Heading 2' # สร้าง style(รูปแบบตัวอักษร) ให้กับข้อมูล
SECONDLINE = document.add_paragraph(scale) # โค้ดส่วนที่ 2 ของการเพิ่มข้อมูลลงใน word
filename = ('word.docx') # ชื่อไฟล์ที่ต้องการจะบันทึก
filepath = r'C:/Users/ksdkd/Desktop/'+filename # ที่อยู่ของไฟล์ที่ต้องการจะบันทึก
document.save(filepath) # บันทึกไฟล์ word