pythonコード =" from docx import Document from docx.shared import Pt, Inches from docx.enum.text import WD_ALIGN_PARAGRAPH from datetime import datetime from docx.oxml import OxmlElement from docx.oxml.ns import qn
# Create a new Word document doc = Document()
# Set document margins (1 inch all around) 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)
# Title doc.add_heading('見積書', level=1).alignment = WD_ALIGN_PARAGRAPH.CENTER
# Company information (right aligned) company_info = "適切な会社情報を設定してください" company_paragraph = doc.add_paragraph(company_info) company_paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT
# Add a table for the quotation items table = doc.add_table(rows=1, cols=4) table.style = 'Table Grid'
# Table headers with background color hdr_cells = table.rows[0].cells hdr_texts = ['品番・品名', '数量', '単価', '金額'] for cell, text in zip(hdr_cells, hdr_texts): cell.text = text cell.paragraphs[0].runs[0].font.bold = True cell.paragraphs[0].runs[0].font.size = Pt(11) # Set cell background color shading_elm = OxmlElement('w:shd') shading_elm.set(qn('w:fill'), 'D3D3D3') cell._tc.get_or_add_tcPr().append(shading_elm)
# Quotation items items = [ 適切な請求内容を設定してください ]
total = sum(item[3] for item in items) tax = total * 0.1 grand_total = total + tax
for name, qty, price, amount in items: row_cells = table.add_row().cells row_cells[0].text = name row_cells[1].text = str(qty) row_cells[2].text = "{:,}".format(price) row_cells[3].text = "{:,}".format(amount) for cell in row_cells: cell.paragraphs[0].runs[0].font.size = Pt(11)
# Add total, tax, and grand total rows for label, value in [("小計", total), ("消費税(10%)", tax), ("合計金額", grand_total)]: row_cells = table.add_row().cells row_cells[2].text = label row_cells[3].text = "{:,}".format(int(value)) for cell in row_cells[2:]: cell.paragraphs[0].runs[0].font.size = Pt(11)
# Save the document file_path = "/mnt/data/見積書_クライアント名.docx" doc.save(file_path)