Python
import
docx
from
docx.shared import Pt, Inches
from
docx.enum.text import WD_ALIGN_PARAGRAPH
#
Create a new document
doc
= docx.Document()
#
Adjust margins to give plenty of space for right-aligned page numbers
for
section in doc.sections:
section.top_margin = Inches(1)
section.bottom_margin = Inches(1)
section.left_margin = Inches(1)
section.right_margin = Inches(1)
#
Helper function to add a line with text and right-aligned page number using a
tab stop
def
add_toc_line(doc, text, page_num, is_heading=False):
p = doc.add_paragraph()
p.paragraph_format.line_spacing = 1.15
p.paragraph_format.space_after = Pt(6)
# Configure a right-aligned tab stop at the
right margin (6.5 inches from left margin given 1-inch margins on 8.5"
page)
# 8.5 - 1 (left) - 1 (right) = 6.5 inches
available width
tab_stops = p.paragraph_format.tab_stops
tab_stops.add_tab_stop(Inches(6.5),
docx.enum.text.WD_TAB_ALIGNMENT.RIGHT)
if is_heading:
p.paragraph_format.space_before = Pt(12)
run = p.add_run(f"{text}\t{page_num}")
run.font.name = 'Calibri'
run.font.size = Pt(11)
run.bold = True
else:
# Indent the chapter titles slightly
for clear hierarchy
p.paragraph_format.left_indent =
Inches(0.4)
run = p.add_run(f"{text}\t{page_num}")
run.font.name = 'Calibri'
run.font.size = Pt(11)
#
Content from image 02.jpg and 03.jpg
#
Rule check: No decorative symbols, stars, dots, hashes, or automatic bullets.
Plain text format with right-aligned page numbers.
add_toc_line(doc,
"Preface", "vii", is_heading=True)
add_toc_line(doc,
"I THE INSTITUTION FRAMEWORK", "5", is_heading=True)
add_toc_line(doc,
"The Logic of Political Sociology", "5")
add_toc_line(doc,
"Mass Communications and the Political Process", "36")
add_toc_line(doc,
"II SOCIAL STRUCTURE AND POLITICAL AFFILIATION", "71",
is_heading=True)
add_toc_line(doc,
"Social and Political Consequences of Social Mobility", "71")
add_toc_line(doc,
"Social Cleavage and Party Affiliation Germany, Great Britain, and the
United States", "88")
add_toc_line(doc,
"III MACROSOCIOLOGY AND ARMED CONFLICT", "119", is_heading=True)
add_toc_line(doc,
"Military Elites and the Study of War", "119")
add_toc_line(doc,
"Civil-Military Relations in the New Nations", "135")
add_toc_line(doc,
"IV POLITICAL VIOLENCE IN AMERICAN INSTITUTIONS", "149",
is_heading=True)
add_toc_line(doc,
"Native Fascism in the 1930's", "149")
add_toc_line(doc,
"Patterns of Collective Racial Violence", "171")
add_toc_line(doc,
"V METHODOLOGICAL DIMENSIONS", "207", is_heading=True)
add_toc_line(doc,
"Content Analysis and the Study of the \"Political
Environment\"", "207")
add_toc_line(doc,
"Community Power and \"Policy Science\" Research", "223")
add_toc_line(doc,
"VI SOCIOLOGICAL ANALYSIS AND POLICY", "243", is_heading=True)
add_toc_line(doc,
"Sociological Models and Social Policy", "243")
add_toc_line(doc,
"The Ideology of Professional Psychologists", "260")
add_toc_line(doc,
"Acknowledgments", "269", is_heading=True)
#
Save the document
file_path
= "Table_of_Contents.docx"
doc.save(file_path)
print(f"File
saved successfully as {file_path}")
Code
output
File
saved successfully as Table_of_Contents.docx
Your
Word document (DOCX) file is ready
![]()
Preface
vii
I
THE INSTITUTION FRAMEWORK
The
Logic of Political Sociology 5
Mass
Communications and the Political Process 36
II
SOCIAL STRUCTURE AND POLITICAL AFFILIATION
Social
and Political Consequences of Social Mobility 71
Social
Cleavage and Party Affiliation Germany, Great Britain, and the United States 88
III
MACROSOCIOLOGY AND ARMED CONFLICT
Military
Elites and the Study of War 119
Civil-Military
Relations in the New Nations 135
IV
POLITICAL VIOLENCE IN AMERICAN INSTITUTIONS
Native
Fascism in the 1930's 149
Patterns
of Collective Racial Violence 171
V
METHODOLOGICAL DIMENSIONS
Content
Analysis and the Study of the "Political Environment" 207
Community
Power and "Policy Science" Research 223
VI
SOCIOLOGICAL ANALYSIS AND POLICY
Sociological
Models and Social Policy 243
The
Ideology of Professional Psychologists 260
Acknowledgments
269