Skip to content

Instantly share code, notes, and snippets.

@monodera
Created April 11, 2019 08:37
Show Gist options
  • Save monodera/87c7b21b560854d42332c154172390e3 to your computer and use it in GitHub Desktop.
Save monodera/87c7b21b560854d42332c154172390e3 to your computer and use it in GitHub Desktop.
エクセルとpythonのサンプル
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import calendar\n",
"from datetime import date\n",
"import xlwings as xw"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"wb = xw.Book(u\"/Users/hoge/Dropbox/2016/業務日誌/gyomu_nisshi_2016_hoge.xls\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# define the year and month to edit\n",
"year = 2016\n",
"month = 9\n",
"\n",
"# don't touch range\n",
"if month <= 8:\n",
" print(\"Don't touch this month 2016!\")\n",
" exit()\n",
"\n",
"# activate the sheet and put my name\n",
"xw.Sheet(u'%i月' % month).activate()\n",
"xw.Range('I4').value = u'氏名[ 名前 ]'"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# dates start from the 9th row\n",
"row_date_start, row_date_end = 9, calendar.monthrange(year, month)[1] + 8 # in the case for \n",
"\n",
"row_dates = np.arange(row_date_start, row_date_end+1)\n",
"dates = row_dates - 9 + 1\n",
"\n",
"# set default arrival/departure times\n",
"time_arrive_default = \"9:30\"\n",
"time_depart_default = \"18:30\"\n",
"\n",
"# set default category of the work (業務)\n",
"gyomu_default = 'E'\n",
"\n",
"# loop on dates to change all cells to the default entry\n",
"for i, r in enumerate(row_dates):\n",
" weekday_index = date(year, month, dates[i]).weekday()\n",
" if (weekday_index != 5 and weekday_index != 6) and (not xw.Range('M%i' % r).value):\n",
" xw.Range('C%i' % r).value = time_arrive_default\n",
" xw.Range('D%i' % r).value = time_depart_default\n",
" xw.Range(u'%s%i' % (gyomu_default, r)).value = u'○'\n",
" else:\n",
" xw.Range('C%i' % r).value = ':'\n",
" xw.Range('D%i' % r).value = ':'\n",
" xw.Range(u'%s%i' % (gyomu_default, r)).value = u''"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# save changes\n",
"wb.save()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.5.6"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment