공부/파이썬 Python
Python에서 워드 파일 처리하기ㅣDocumentㅣdocxㅣWord File Processing in Python
혼밥맨
2022. 6. 25. 23:55
반응형
Python에서 워드 파일 처리하기ㅣDocumentㅣdocxㅣWord File Processing in Python
python-docx
https://python-docx.readthedocs.io/en/latest/
python-docx — python-docx 0.8.11 documentation
from docx import Document from docx.shared import Inches document = Document() document.add_heading('Document Title', 0) p = document.add_paragraph('A plain paragraph having some ') p.add_run('bold').bold = True p.add_run(' and some ') p.add_run('italic.')
python-docx.readthedocs.io
python-docx는 Microsoft Word(.docx) 파일을 만들고 업데이트하기 위한 Python 라이브러리입니다. Document 라이브러리는 사용자와 동료가 파일을 쉽게 찾고 함께 작업하며 모든 장치에서 언제든지 액세스할 수 있는 안전한 저장 공간을 제공합니다. 예를 들어 쉐어포인트 사이트의 문서 라이브러리를 사용하여 특정 프로젝트 또는 특정 클라이언트와 관련된 모든 파일을 저장할 수 있습니다.
python-docx is a Python library for creating and updating Microsoft Word (.docx) files.
import libraries
1
2
|
from docx import Document
import paragraphs
|
cs |
Sample Code #01 (Writing "Hello World" in Docx format)
1
2
3
4
5
6
|
from docx import Document
import paragraphs
document = Document()
document.add_heading("Hello World", 1)
document.save("test.docx")
|
cs |
Sample Code #02 (Adding list bullets)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
from docx import Document
import paragraphs
document = Document()
document.add_heading("Hello World", 0)
p.document.add_paragraph("This is a sample text!")
p.add_run(" This text is bold.").bold = True
p.add_run(" This text is italic.").italic = True
document.add_paragraph("This is item #01", style="List Bullet")
document.add_paragraph("This is item #02", style="List Bullet")
document.add_paragraph("This is item #03", style="List Bullet")
document.add_paragraph("This is item #04", style="List Bullet")
document.add_paragraph("This is item #05", style="List Bullet")
document.save("test.docx")
|
cs |
Sample Code #03 (Adding a talbe to #02)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
from docx import Document
import paragraphs
document = Document()
document.add_heading("Hello World", 0)
p.document.add_paragraph("This is a sample text!")
p.add_run(" This text is bold.").bold = True
p.add_run(" This text is italic.").italic = True
document.add_paragraph("This is item #01", style="List Bullet")
document.add_paragraph("This is item #02", style="List Bullet")
document.add_paragraph("This is item #03", style="List Bullet")
document.add_paragraph("This is item #04", style="List Bullet")
document.add_paragraph("This is item #05", style="List Bullet")
table_header = ["Name", "Age", "Job"]
some_data = [
["John", 46, "Programmer"],
["Mary", 55, "Programmer"],
["Anna", 27, "Programmer"],
["bob", 50, "Chef"]
]
table = document.add_table(rows=1, cols=3)
for i in range(3):
table.rows[0].cells[i].text = table_header[i]
for name, age, job in some_data:
cells = table.add_row().cells
cells[0].text = name
cells[1].text = str(age)
cells[2].text = job
document.save("test.docx")
|
cs |
반응형