ทำกาติดตั้ง library docx ซึ่งจะมีคำสั่งติดตั้ง python-docx ดังนี้
โค้ด: เลือกทั้งหมด
pip3 install python-docx
โค้ด: เลือกทั้งหมด
from docx import Document # Library ของ docx ที่ใช้
from docx.shared import Inches, Cm # Library ของ docx เป็นหน่วยนิ้ว และเซนติเมตร
from docx.shared import Pt
document = Document()
name = "Yourname Lastname" # สร้างชื่อตัวแปรในรูปแบบ String
print ("create WORD.....")
FIRSTLINE = document.add_paragraph(name) # โค้ดส่วนของการเพิ่มข้อมูลลงใน word
paragraph_format = FIRSTLINE.paragraph_format
FIRSTLINE.style = 'Heading 2' # สร้าง style(รูปแบบตัวอักษร) ให้กับข้อมูล
filename = ('word.docx') # ชื่อไฟล์ที่ต้องการจะบันทึก
filepath = r'C:/Users/ksdkd/Desktop/'+filename # ที่อยู่ของไฟล์ที่ต้องการจะบันทึก
document.save(filepath) # บันทึกไฟล์ word