Skip to content

Instantly share code, notes, and snippets.

@dev001hajipro
Created October 12, 2017 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dev001hajipro/9308527cb2495f2c05f04690710dca37 to your computer and use it in GitHub Desktop.
Save dev001hajipro/9308527cb2495f2c05f04690710dca37 to your computer and use it in GitHub Desktop.
COMを使ってPythonからExcelを操作する簡単なソースコード
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""COMでPythonからExcelを操作する
"""
import os
from win32com.client import Dispatch
def main():
print(os.getcwd())
filename = os.getcwd() + '\hello_excel.xlsx'
# 存在する場合は削除
if os.path.exists(filename):
os.remove(filename)
xl = Dispatch('Excel.Application')
print(xl)
wb = xl.Workbooks.Add()
ws = wb.Worksheets.Add()
cell = ws.Cells(1)
cell.Value = 'こんにちは、Python(UTF-8)で書き込み世界!'
wb.Close(True, filename)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment