{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Gunakan Library yang dibutuhkan" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from openpyxl import load_workbook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Load file Excel\n", "Ubah lokasi file dari **/home/aan/basangdata_excel/python_menjumlahkan_baris/contoh_output_formula.xlsx** menjadi lokasi yang sesuai, misal **d:\\latihan_excel_python\\contoh_output_formula.xlsx**" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "wb = load_workbook('/home/aan/basangdata_excel/python_menjumlahkan_baris/contoh_output_formula.xlsx', data_only = True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Buka semua Sheet selain Total\n", "Di tiap sheet, baca semua baris pada **kolom B**, jika menemukan kata **Jumlah Periode Ini** simpan **nama sheet** dan **nomor baris** ke dalam variable **row_numbers**" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "row_numbers = {}\n", "for ws in wb.worksheets:\n", " if 'Total' not in ws.title:\n", " max_row_b = ws.max_row + 1\n", " \n", " for row_b in range(2, max_row_b):\n", " if ws['B' + str(row_b)].value == 'Jumlah Periode Ini':\n", " row_numbers[ws.title] = row_b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Buat formula\n", "Variable **ws_total** adalah worksheet **Total**" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ws_total = wb['Total']\n", "formula = '='" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Tambahkan formula\n", "berdasarkan semua **nama sheet** dan **baris** dimana ditemukan teks **Jumlah Periode Ini**" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for rn in row_numbers:\n", " formula = formula + rn + '!H' + str(row_numbers[rn]) + '+'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Tulis formula\n", "di sheet **Total** sel **B2** dengan teks seperti variable formula namun **dikurangi** 1 karakter terakhir (**tanda +**)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ws_total['B2'].value = formula[:-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Simpan file Excel\n", "Dengan nama **contoh_output_formula_hasil.xlsx**, silahkan ubah lokasi file agar sesuai" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "wb.save('/home/aan/basangdata_excel/python_menjumlahkan_baris/contoh_output_formula_hasil.xlsx')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }