{"id":176,"date":"2015-06-14T21:09:56","date_gmt":"2015-06-14T13:09:56","guid":{"rendered":"http:\/\/vinta.ws\/code\/?p=176"},"modified":"2026-02-18T01:20:36","modified_gmt":"2026-02-17T17:20:36","slug":"python-with-excel-xlrd-xlsxwriter-and-xlutils","status":"publish","type":"post","link":"https:\/\/vinta.ws\/code\/python-with-excel-xlrd-xlsxwriter-and-xlutils.html","title":{"rendered":"Python with Excel: xlrd, xlsxwriter, and xlutils"},"content":{"rendered":"<h2>Libraries<\/h2>\n<p><code>xlsxwriter<\/code> \u7684\u6587\u4ef6\u5beb\u5f97\u6bd4\u8f03\u597d<\/p>\n<p>ref:<br \/>\n<a href=\"http:\/\/www.python-excel.org\/\">http:\/\/www.python-excel.org\/<\/a><br \/>\n<a href=\"https:\/\/xlsxwriter.readthedocs.org\/en\/latest\/\">https:\/\/xlsxwriter.readthedocs.org\/en\/latest\/<\/a><br \/>\n<a href=\"http:\/\/openpyxl.readthedocs.org\/en\/latest\/\">http:\/\/openpyxl.readthedocs.org\/en\/latest\/<\/a><\/p>\n<h2>Usage<\/h2>\n<p>row \u662f\u6a6b\u6392<br \/>\ncolumn \u662f\u76f4\u6392<\/p>\n<h3>Default format<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-py\">import xlsxwriter\n\nworkbook = xlsxwriter.Workbook('label_copy.xlsx')\n\n# default cell format\nworkbook.formats[0].set_font_size(12)\nworkbook.formats[0].set_text_wrap() # \u8981\u52a0\u4e0a\u9019\u500b\u624d\u80fd\u6b63\u5e38\u986f\u793a\u591a\u884c\nworkbook.formats[0].set_align('vcenter')<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/xlsxwriter.readthedocs.org\/en\/latest\/format.html\">https:\/\/xlsxwriter.readthedocs.org\/en\/latest\/format.html<\/a><\/p>\n<h3>Multiple lines<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-py\">lines_format = workbook.add_format({\n    'align': 'left',\n    'font_size': 12,\n    'text_wrap': True,\n    'valign': 'vcenter',\n})\n\n# \u6216\u662f\u7528 \"\"\"\u591a\u884c\"\"\"\ncontent = 'first linensecond line'\nworksheet.write(0, 0, content, lines_format)<\/code><\/pre>\n<p>\u91cd\u9ede\u662f\u8981\u52a0\u4e0a <code>text_wrap<\/code><\/p>\n<p>ref:<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/15370432\/writing-multi-line-strings-into-cells-using-openpyxl\">http:\/\/stackoverflow.com\/questions\/15370432\/writing-multi-line-strings-into-cells-using-openpyxl<\/a><\/p>\n<h3>Write to existing excel files<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-py\">from xlutils.copy import copy as xlutils_copy\nimport xlrd\n\nrb = xlrd.open_workbook('your_file.xls', formatting_info=True)\nwb = xlutils_copy(rb)\nws = wb.get_sheet(0)\nws.write(0, 0, 'Hello World')<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/2725852\/writing-to-existing-workbook-using-xlwt\">https:\/\/stackoverflow.com\/questions\/2725852\/writing-to-existing-workbook-using-xlwt<\/a><\/p>\n<h2>Examples<\/h2>\n<p>ref:<br \/>\n<a href=\"https:\/\/xlsxwriter.readthedocs.org\/en\/latest\/examples.html\">https:\/\/xlsxwriter.readthedocs.org\/en\/latest\/examples.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>xlsxwriter \u7684\u6587\u4ef6\u5beb\u5f97\u6bd4\u8f03\u597d<\/p>\n","protected":false},"author":1,"featured_media":774,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[2],"class_list":["post-176","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-about-python","tag-python"],"_links":{"self":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/comments?post=176"}],"version-history":[{"count":0,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/176\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media\/774"}],"wp:attachment":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media?parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/categories?post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/tags?post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}