Загрузка данных


import zipfile

# 1. Формируем XML для каждого листа
# Лист 1: Табулирование
s1_rows = [
    '<row r="1">'
    '<c r="A1" t="inlineStr"><is><t>x</t></is></c>'
    '<c r="B1" t="inlineStr"><is><t>y = F(x)</t></is></c>'
    "</row>"
]
for i in range(11):
    r = i + 2
    x_val = round(i * 0.05, 2)
    s1_rows.append(
        f'<row r="{r}">'
        f"<c r=\"A{r}\"><v>{x_val}</v></c>"
        f"<c r=\"B{r}\"><f>A{r}-0.21*SIN(0.5+A{r})</f></c>"
        f"</row>"
    )
s1_rows.append(
    '<row r="14"><c r="A14" t="inlineStr"><is><t>Вывод: смена знака между 0.1 и 0.2. Корень на отрезке [0.1; 0.2]</t></is></c></row>'
)
sheet1_xml = (
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
    '<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
    "<sheetData>" + "".join(s1_rows) + "</sheetData>"
    "</worksheet>"
)

# Лист 2: Деление пополам
s2_rows = [
    '<row r="1">'
    '<c r="A1" t="inlineStr"><is><t>a (левая)</t></is></c>'
    '<c r="B1" t="inlineStr"><is><t>c (центр)</t></is></c>'
    '<c r="C1" t="inlineStr"><is><t>b (правая)</t></is></c>'
    '<c r="D1" t="inlineStr"><is><t>F(a)</t></is></c>'
    '<c r="E1" t="inlineStr"><is><t>F(c)</t></is></c>'
    '<c r="F1" t="inlineStr"><is><t>F(b)</t></is></c>'
    '<c r="G1" t="inlineStr"><is><t>b-a (погрешность)</t></is></c>'
    '<c r="H1" t="inlineStr"><is><t>k (итерация)</t></is></c>'
    "</row>"
]
# Первая итерация
s2_rows.append(
    '<row r="2">'
    '<c r="A2"><v>0.1</v></c>'
    '<c r="B2"><f>(A2+C2)/2</f></c>'
    '<c r="C2"><v>0.2</v></c>'
    '<c r="D2"><f>A2-0.21*SIN(0.5+A2)</f></c>'
    '<c r="E2"><f>B2-0.21*SIN(0.5+B2)</f></c>'
    '<c r="F2"><f>C2-0.21*SIN(0.5+C2)</f></c>'
    '<c r="G2"><f>C2-A2</f></c>'
    '<c r="H2"><v>1</v></c>'
    "</row>"
)
# Со 2 по 11 итерации
for r in range(3, 13):
    p = r - 1
    s2_rows.append(
        f'<row r="{r}">'
        f"<c r=\"A{r}\"><f>IF(D{p}*E{p}&lt;0,A{p},B{p})</f></c>"
        f"<c r=\"B{r}\"><f>(A{r}+C{r})/2</f></c>"
        f"<c r=\"C{r}\"><f>IF(D{p}*E{p}&lt;0,B{p},C{p})</f></c>"
        f"<c r=\"D{r}\"><f>A{r}-0.21*SIN(0.5+A{r})</f></c>"
        f"<c r=\"E{r}\"><f>B{r}-0.21*SIN(0.5+B{r})</f></c>"
        f"<c r=\"F{r}\"><f>C{r}-0.21*SIN(0.5+C{r})</f></c>"
        f"<c r=\"G{r}\"><f>C{r}-A{r}</f></c>"
        f"<c r=\"H{r}\"><f>H{p}+1</f></c>"
        f"</row>"
    )
s2_rows.append(
    '<row r="14"><c r="A14" t="inlineStr"><is><t>ОТВЕТ: x ≈ 0.1224 (на 10 шаге b-a &lt;= 0.0001)</t></is></c></row>'
)
sheet2_xml = (
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
    '<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
    "<sheetData>" + "".join(s2_rows) + "</sheetData>"
    "</worksheet>"
)

# Лист 3: Метод Ньютона
s3_rows = [
    '<row r="1">'
    '<c r="A1" t="inlineStr"><is><t>n</t></is></c>'
    '<c r="B1" t="inlineStr"><is><t>x_n</t></is></c>'
    '<c r="C1" t="inlineStr"><is><t>f(x_n)</t></is></c>'
    '<c r="D1" t="inlineStr"><is><t>f\'(x_n)</t></is></c>'
    '<c r="E1" t="inlineStr"><is><t>x_(n+1)</t></is></c>'
    '<c r="F1" t="inlineStr"><is><t>|x_(n+1) - x_n|</t></is></c>'
    "</row>",
    '<row r="2">'
    '<c r="A2"><v>0</v></c>'
    '<c r="B2"><v>0.2</v></c>'
    '<c r="C2"><f>B2-0.21*SIN(0.5+B2)</f></c>'
    '<c r="D2"><f>1-0.21*COS(0.5+B2)</f></c>'
    '<c r="E2"><f>B2-C2/D2</f></c>'
    '<c r="F2"><f>ABS(E2-B2)</f></c>'
    "</row>",
]
for r in range(3, 7):
    p = r - 1
    s3_rows.append(
        f'<row r="{r}">'
        f"<c r=\"A{r}\"><f>A{p}+1</f></c>"
        f"<c r=\"B{r}\"><f>E{p}</f></c>"
        f"<c r=\"C{r}\"><f>B{r}-0.21*SIN(0.5+B{r})</f></c>"
        f"<c r=\"D{r}\"><f>1-0.21*COS(0.5+B{r})</f></c>"
        f"<c r=\"E{r}\"><f>B{r}-C{r}/D{r}</f></c>"
        f"<c r=\"F{r}\"><f>ABS(E{r}-B{r})</f></c>"
        f"</row>"
    )
s3_rows.append(
    '<row r="8"><c r="A8" t="inlineStr"><is><t>ОТВЕТ: x ≈ 0.122428 (точность 10^-5 достигнута)</t></is></c></row>'
)
sheet3_xml = (
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
    '<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
    "<sheetData>" + "".join(s3_rows) + "</sheetData>"
    "</worksheet>"
)

# Лист 4: Поиск решения
sheet4_xml = (
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
    '<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
    "<sheetData>"
    '<row r="1">'
    '<c r="A1"><f>C1-0.21*SIN(0.5+C1)</f></c>'
    '<c r="C1"><v>0.1</v></c>'
    "</row>"
    '<row r="3"><c r="A3" t="inlineStr"><is><t>ИНСТРУКЦИЯ ДЛЯ ПОИСКА РЕШЕНИЯ:</t></is></c></row>'
    '<row r="4"><c r="A4" t="inlineStr"><is><t>1. Вкладка Данные -> Поиск решения</t></is></c></row>'
    '<row r="5"><c r="A5" t="inlineStr"><is><t>2. Целевая ячейка: $A$1, значение: 0</t></is></c></row>'
    '<row r="6"><c r="A6" t="inlineStr"><is><t>3. Изменяя ячейки: $C$1 -> Нажать Найти решение</t></is></c></row>'
    '<row r="8"><c r="A8" t="inlineStr"><is><t>Корень запишется в C1: ≈ 0.122428</t></is></c></row>'
    "</sheetData>"
    "</worksheet>"
)

# 2. Системные файлы Excel
content_types = (
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
    '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">'
    '<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>'
    '<Default Extension="xml" ContentType="application/xml"/>'
    '<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>'
    '<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>'
    '<Override PartName="/xl/worksheets/sheet2.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>'
    '<Override PartName="/xl/worksheets/sheet3.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>'
    '<Override PartName="/xl/worksheets/sheet4.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>'
    '<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>'
    "</Types>"
)

rels = (
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
    '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
    '<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>'
    "</Relationships>"
)

workbook_xml = (
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
    '<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">'
    "<sheets>"
    '<sheet name="Табулирование" sheetId="1" r:id="rId1"/>'
    '<sheet name="Деление пополам" sheetId="2" r:id="rId2"/>'
    '<sheet name="Метод Ньютона" sheetId="3" r:id="rId3"/>'
    '<sheet name="Поиск решения" sheetId="4" r:id="rId4"/>'
    "</sheets>"
    "</workbook>"
)

workbook_rels = (
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
    '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
    '<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>'
    '<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet2.xml"/>'
    '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet3.xml"/>'
    '<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet4.xml"/>'
    '<Relationship Id="rIdStyle" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>'
    "</Relationships>"
)

styles_xml = (
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
    '<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
    '<fonts count="1"><font><sz val="11"/><name val="Calibri"/></font></fonts>'
    '<fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills>'
    '<borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders>'
    '<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>'
    '<cellXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/></cellXfs>'
    "</styleSheet>"
)

# 3. Сохранение файла XLSX
filename = "Вариант_7_Практическая_2.xlsx"
with zipfile.ZipFile(filename, "w", zipfile.ZIP_DEFLATED) as z:
    z.writestr("[Content_Types].xml", content_types)
    z.writestr("_rels/.rels", rels)
    z.writestr("xl/workbook.xml", workbook_xml)
    z.writestr("xl/_rels/workbook.xml.rels", workbook_rels)
    z.writestr("xl/styles.xml", styles_xml)
    z.writestr("xl/worksheets/sheet1.xml", sheet1_xml)
    z.writestr("xl/worksheets/sheet2.xml", sheet2_xml)
    z.writestr("xl/worksheets/sheet3.xml", sheet3_xml)
    z.writestr("xl/worksheets/sheet4.xml", sheet4_xml)

print(f"УСПЕХ! Файл '{filename}' создан.")