2017-05-01 4 views
1

python-docx로 Word 문서를 만들어 봅니다. 작성된 파일은 문자 크기 8.5 x 11 인치입니다. 그러나 독일에서는 표준 형식이 A4 8.27 x 11.69 인치입니다.python-docx에서 페이지 크기를 A4로 변경하는 방법

from docx import Document 
from docx.shared import Inches 

document = Document() 

document.add_heading('Document Title', 0) 
document.settings 


p = document.add_paragraph('A plain paragraph having some ') 
p.add_run('bold').bold = True 
p.add_run(' and some ') 
p.add_run('italic.').italic = True 

document.add_heading('Heading, level 1', level=1) 
document.add_paragraph('Intense quote', style='IntenseQuote') 

document.add_paragraph(
    'first item in unordered list', style='ListBullet' 
) 
document.add_paragraph(
    'first item in ordered list', style='ListNumber' 
) 



table = document.add_table(rows=1, cols=3) 
hdr_cells = table.rows[0].cells 
hdr_cells[0].text = 'Qty' 
hdr_cells[1].text = 'Id' 
hdr_cells[2].text = 'Desc' 


document.add_page_break() 

document.save('demo.docx') 

설명서에서이 항목에 대한 정보를 찾을 수 없습니다.

답변

1

나는 당신이 documentation에서,이 원하는 생각합니다. 절에

세 속성은 페이지 크기 및 방향을 설명한다. 는 함께 이러한 세로에서 가로로 섹션의 방향을 변경, 예를 들어, 사용할 수 있습니다

>>> section.orientation, section.page_width, section.page_height 
(PORTRAIT (0), 7772400, 10058400) # (Inches(8.5), Inches(11)) 
>>> new_width, new_height = section.page_height, section.page_width 
>>> section.orientation = WD_ORIENT.LANDSCAPE 
>>> section.page_width = new_width 
>>> section.page_height = new_height 
>>> section.orientation, section.page_width, section.page_height 
(LANDSCAPE (1), 10058400, 7772400) 
3

Documentpage_heightpage_width 속성을 여러 Section 강철로 만들어진 것 같습니다.

는 A4의 첫 번째 섹션의 크기를 설정하려면 (테스트되지 않은)을 시도 할 수 : A4는 millimeters에 정의되어 있는지

section = document.sections[0] 
section.page_height = Mm(297) 
section.page_width = Mm(210) 

참고.