Create a gist now

Instantly share code, notes, and snippets.

Embed
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