Python2.5ã§Excelã使ããã«Excelãã¡ã¤ã«ãèªã¿æ¸ãããã
åæ©
ä¸è¨ã§JAVAã試ãããããã¨ããããã§ã¯ãªãã¦ããããããã¡ããç®çã
Pythonã®ãããªè»½éè¨èªã§ã¯ã´ãªã´ãªä½ãããããããå©ç¨ã§ãããã®ã¯å©ç¨ãã¦ãã¾ã£ãã»ããããã¨æããããã§Excelã使ã£ã¦å
¥åãã¼ã¿ãä½ã£ã¦ããPythonã§èªã¿è¾¼ã¾ãããããããã¯Pythonã§éè¨ããçµæãExcelã§åºåã§ãããè¯ãããã ãªãã¨ããã¨ãããåæ©ã
ãã¡ã¤ã«å½¢å¼ã¨ãã¦ã¯CSVãã¡ã¤ã«ã§ãè¯ãã£ããããã®ã ããããã¯æ¡å¤é£ããããExcelã®è¤æ°ã·ã¼ãã®æ±ãããåºåããã¨ãã¦ãè²ã¥ãã罫ç·ã¥ãã¯é
åçã ã¨æãã
æºå
Pythonã§Excelãã¡ã¤ã«ãæ±ãããã®ã©ã¤ãã©ãªã¨ãã¦ãpyExceleratorã¨ãããã®ãããããããããã使ãã¨ãExcelã®èªã¿æ¸ããç°¡åã«ã§ããã
http://sourceforge.net/projects/pyexcelerator/
ã©ã¤ã»ã³ã¹ã¯BSD Lisenceã§ããã
- ãã¦ã³ãã¼ããã¼ã¸ãããpyExcelerator-xxx.zipãåå¾ããã
- ãããå±éããsetupãå®è¡ããã
python setup.py install
ããã§pyExceleratorãã¤ã³ã¹ãã¼ã«ãããã
Excelãã¡ã¤ã«ãä½æãã¦ã¿ãã
Excelãã¡ã¤ã«ãExcelã使ããã«Pythonããä½æãã¦ã¿ããã¹ãã
#!/usr/bin/env python # -*- coding: utf-8 -*- import pyExcelerator # ã¹ã¿ã¤ã«ã®ãã£ãã·ã¥ styles = {}; # ãã¿ã¼ã³çªå·ããã¹ã¿ã¤ã«ãçæããã£ãã·ã¥ãã. # åãã¹ã¿ã¤ã«ã¨ãªãå ´åã¯çææ¸ã¿ã®ãã®ã使ç¨ãã. # (Excel97-2003å½¢å¼ã§ä½¿ããã¹ã¿ã¤ã«æ°ã¯4000åã¾ã§ã®ãã # åãã¹ã¿ã¤ã«ã®å ´åã¯æ°ããã¹ã¿ã¤ã«ãä½ããªããã¨ã§ç¯ç´ãã.) # http://office.microsoft.com/ja-jp/excel-help/HA010077823.aspx def findOrCreateStyle(cnt): # ãã¿ã¼ã³çªå·ãããã«ã©ã¼ã¨ç½«ç·ã®çµã¿åãããã¼ãä½æãã. # (è¦ãç®ã段éãã«ãªãããã«å²ãåããªãããã«ãã¦ãã.) color = cnt % 9 border = cnt % 3 key = (color, border) # ãã¼ã«å¯¾ããã¹ã¿ã¤ã«ãæªä½æã®å ´åã®ã¿ã¹ã¿ã¤ã«ãä½æãã. if key not in styles: style = pyExcelerator.XFStyle() style.num_format_str = "@" # æååãã©ã¼ããã # ã»ã«ã®æ°´å¹³ã»åç´æããä¸å¤®ã«ãã alignment = pyExcelerator.Formatting.Alignment() alignment.horz = pyExcelerator.Formatting.Alignment.HORZ_CENTER alignment.vert = pyExcelerator.Formatting.Alignment.VERT_CENTER style.alignment = alignment # 罫ç·ãã¤ãã if border in (1, 2): borders = pyExcelerator.Formatting.Borders() if border == 1: borders.bottom = pyExcelerator.Formatting.Borders.MEDIUM if border == 2: borders.left = pyExcelerator.Formatting.Borders.THIN style.borders = borders font = pyExcelerator.Formatting.Font() # å®ç¾©è²: 0é» 1ç½ 2赤 3ç· 4é 5é» 6ãã¼ã³ã 7ã·ã¢ã³ # 詳細ã¯BIFFRecords.py åç § font.colour_index = color % 8 font.bold = True # 太å font.height = 16 * 20 # 16pt (1/20ptåä½ã§æå®ãã) style.font = font # ã¹ã¿ã¤ã«ã®ä¿å styles[key] = style # ä½ææ¸ã¿ã¹ã¿ã¤ã«ãè¿ã. return styles[key] def main(): # æ°ããããã¯ãã¡ã¢ãªä¸ã«ä½æãã workbook = pyExcelerator.Workbook() cnt = 0 # æ°ããã·ã¼ããä½æããã for sheetNo in range(0, 10): # UNICODEã§ã·ã¼ãã¿ã¤ãã«æå® worksheet = workbook.add_sheet(u"ã·ã¼ã%d" % sheetNo) # è¡ x å ã§ã»ã«ãåãã for row in range(0, 10): # 1è¡ç®ã¯0 for col in range(0, 20): # Aåã¯0 # ã¹ã¿ã¤ã«ãè¨å® style = findOrCreateStyle(cnt) cnt += 1 # ã»ã«ã«æ¸ã込㿠worksheet.write(row, col, label = u"%d表%dè¡%då" % (sheetNo, row, col), style = style) # åå¹ ãè¨å®ãã for col in range(0, 20): # 15æåå¹ (ããã©ã«ãã®ãã©ã³ãã§æåã0ãã®1/256ãåä½ã¨ãã) worksheet.col(col).width = 256 * 15 # ããã¯ããã¡ã¤ã«ã«ä¿åããã(æé»ã§ä¸æ¸ãä¿å) workbook.save(u"ãã¹ã1.xls") # å®è¡ main()
é©ããã¨ã§ããªãã®ãããããªãããUnicodeæååã¨ãã¦æå®ããããã°æååãããããã¨ãªãæ¥æ¬èªãæ®éã«æ±ããã
ããããJavaã®POIã§æ¸ããããã·ã³ãã«ã§ç°¡åãªããã§ããã
ãã®pyExceleratorã®ããã¸ã§ã¯ãã«ã¯ãã¾ã¨ããªããã¥ã¡ã³ãã®ãããªãã®ããªããããªã®ã ã(ãããã¦ãã¿ã¤ãããªã)ããµã³ãã«ãè¦ãã°ã使ãæ¹ã¯ã ãããåããã ããã
ããã§ãåãããªããã°ã½ã¼ã¹ãè¦ãã°è¯ããä¸å¯§ãªã³ã¡ã³ããã¤ãã¦ãã¦ãã¤ãã§ã«Excelã®BIFFãã©ã¼ãããã«ãã¡ãã£ã¨è©³ãããªãããããããªãç¹å
¸ä»ãã§ããã
ã¨ãããããExcelãã¡ã¤ã«ãæ¸ãã®ã¯ãã¨ã¦ãç°¡åã ã¨ããå°è±¡ã
Excelãã¡ã¤ã«ãèªã¿è¾¼ãã§ã¿ã
Excelãã¡ã¤ã«ãExcelã使ããã«Pythonããèªã¿è¾¼ãã§ã¿ããã¹ãã
#!/usr/bin/env python # -*- coding: utf-8 -*- import pyExcelerator def main(): sheets = pyExcelerator.parse_xls(u"ãã¹ã1.xls") for (sheetName, sheet) in sheets: print u"ã·ã¼ãå: %s" % sheetName # ã·ã¼ãã¯æå¹ãªã«ã©ã ããããã(è¡ã»å)ã®ã¿ãã«ããã¼ã¨ãããããã¨ãã¦æ±ãããã # ãã£ã¦ãã¾ããæå¹ãªã«ã©ã ä½ç½®ãåå¾ããè¡åé ã«ä¸¦ã³æ¿ããã keys = sorted(sheet.keys()) for (row, col) in keys: value = sheet[(row, col)] print u"%2dè¡%2då: %s" % (row, col, value) # å®è¡ main()
ããã§Excelã®å
¨ã·ã¼ããå
¨ã»ã«ã®å¤ãæ¨æºåºåã«éãåºããã
ãµã³ãã«ã³ã¼ãã§ã¯cp???ã¨ããå
·åã«ãã£ã©ã¯ã¿ã¼ã»ãããæå®ãã¦ããããçç¥ãã¦ãåé¡ãªãæ¥æ¬èªãèªã¿è¾¼ããã(UTF-16æ±ããªãã¨ã幸ãããã®ã§ããããã)
ãããçããã¨ã«ãªã£ã¦ããããã ããåã¶ã®ã¯æ©è¨ã§ããã
è¦ã¦ã®ã¨ãããExcelã®æ¸ãæ¹ã¨èªã¿æ¹ã¯ããããéãã
æ¸ãæ¹ã§ã¯ãä¸å¿ãã·ã¼ãããã£ã¦è¡ããã£ã¦ã»ã«ããã£ã¦â¦ãã¨ãããªãã¸ã§ã¯ãã®ä½è£ããã£ã¦ããã®ã ããèªã¿è¾¼ã¿ã®ã»ãã¯ã·ã¼ãåãã»ã«ä½ç½®ã表ãã¿ãã«ã¨ã»ã«ãã¼ã¿ã®ã¹ãã¼ã¹é
å*1ã®ãã¢ã¨ãã¦ã解ææ¸ã¿ãã®å½¢ã§åå¾ããããã¨ãããããã»ã«ã®ãã¼ã¿ãèªãä¸ã§ã¯ä¸èªç±ãªãã(ã ãããç§çã«ã¯åé¡ãªãã®ã ãã)
ã ããExcelãã¡ã¤ã«ãéãã¦ç·¨éãã¦æ¸ãæ»ããã¨ãã£ãRoundtripãªä½¿ãæ¹ã¯ç¡çã£ã½ãã®ã§ããã
ã¨ã¯ããã解ææ¸ã¿ã§ãã¹ãã¼ã¹é åãã¨ãã¦æ»ã£ã¦ããã¨ããã¯ãããããããããªããããããä½è¨ãªæéããªãç°¡åã«ç®çã®ã³ã¼ããããããã ããã§ããã
çµè«
POIã¨æ¯ã¹ãã¨æè»æ§ãåºæ¥ããã¨ã¯éå®ããã¦ãã¾ããããã¼ã¿ã®èªã¿æ¸ãã¨ãã¦ä½¿ãåã«ã¯å®ç¨çã§ããã¨æããã
Pythonã§ãé常ã«ç°¡åã«Excelãã¡ã¤ã«ã®èªã¿æ¸ããã§ãããã¨ãããã£ãã
ãã®pyExceleratorã¯æ¯éæ´»ç¨ãã¹ãã©ã¤ãã©ãªã®ããã«æãã
*1:ã·ã¼ãã®ã»ã«ã¯é£ã³å°ã§ããã¾ããªããããã®å ´åããã®ã»ã«éã®ééã¯ãã¼ã¿ã¨ãã¦åå¨ãã¦ããªããpyExceleratorã®Excelãã¡ã¤ã«ã®èªã¿è¾¼ã¿ã§ã¯ãåã¾ã£ã¦ããã»ã«ã ããæ»ãã®ã§ãæå¹ãªã»ã«ã§ãããã®å¤æãã»ã«ãã©ãã¾ã§ããã®ãã¨ãã£ããã§ãã¯ãä¸è¦ã§ããã