ในบทความนี้ มาเรียนรู้การเขียน
ภาษา python โดยการสร้างไฟล์ word ขึ้นมา พร้อมกับใส่ข้อมูลหรือเนื้อหาลงไป
ทำกาติดตั้ง library docx ซึ่งจะมีคำสั่งติดตั้ง python-docx ดังนี้
มักจะใช้กับ python3 **หาก error เกี่ยวกับ version ให้ใช้ (pip3 install --pre 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
ในบทความนี้ มาเรียนรู้การเขียน[url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2417-python-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]ภาษา python[/url] โดยการสร้างไฟล์ word ขึ้นมา พร้อมกับใส่ข้อมูลหรือเนื้อหาลงไป
ทำกาติดตั้ง library docx ซึ่งจะมีคำสั่งติดตั้ง python-docx ดังนี้
[code]pip3 install python-docx[/code]
มักจะใช้กับ python3 **หาก error เกี่ยวกับ version ให้ใช้ (pip3 install --pre python-docx) แทน
[code]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[/code]