๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Python/Basic

[Python] python-docx - word ์ฝ๊ธฐ/ํŽธ์ง‘ ํ•˜๊ธฐ

๋ฐ˜์‘ํ˜•

 

Demo.docx

 

Demo.docx ํŒŒ์ผ์„ ์—ด์–ด์„œ ํŽธ์ง‘ํ•ด๋ณด๊ธฐ

from docx import Document
from docx.enum.table import WD_ALIGN_VERTICAL
from docx.enum.text import WD_ALIGN_PARAGRAPH

document = Document('Demo.docx')
# ๋ฌธ์„œ๋‚ด์˜ ํ…Œ์ด๋ธ”๋“ค
tables = document.tables

# 1๋ฒˆ์งธ ํ…Œ์ด๋ธ” 0, 0 ์…€ - 2๋ฒˆ์งธ paragraphs์— ์ž…๋ ฅ
tables[0].cell(0, 0).paragraphs[1].text = 'paragraphs[1]'

# 2๋ฒˆ์งธ ํ…Œ์ด๋ธ” 0, 0 ์…€์— ์ž…๋ ฅ
tables[1].cell(0, 0).text = 'table[1] (0,0)'

out_file = 'New.docx'
document.save(out_file)

 

 

ํ‘œ์— ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ๋ฐฉ๋ฒ•

1. 

tables[0].rows[0].cells[3].paragraphs[0].text = "test"
tables[0].rows[0].cells[3].paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.RIGHT

 

2. ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ํฐํŠธ์˜ ๊ตต๊ฒŒ, ์ดํƒค๋ฆญ, ๋ฐ‘์ค„์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

tables[0].rows[3].cells[3].paragraphs[0].add_run("test")

 

runner = tables[0].rows[3].cells[4].paragraphs[0].add_run("test4")
runner.bold = True
runner.underline = True
runner.italic = True

 

  • ์…€ ํฌ๊ธฐ ์กฐ์ ˆ
cells[0].width = Cm(2.9)
cells[0].height = Cm(2.9)

 

 

 

https://www.geeksforgeeks.org/working-with-tables-python-docx-module/

 

Working with Tables - Python .docx Module - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

 

 

 

 

 

https://python-docx.readthedocs.io/en/latest/#user-guide

 

python-docx — python-docx 0.8.10 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

 

๋ฐ˜์‘ํ˜•