Tag: xlwt

如何使用XLWT Python Excel将整个样式应用于整行?

我试图应用一种风格,如果其中一列包含值“资产”将突出显示整个行。 下面的代码将仅突出显示“Assets”的列,而不是整行。 有没有办法将整个样式应用到整行? for row in csv_input: #Iterate through each column for col in range(len(row)): #Apply different styles depending on row if row_count == 0: sheet.write(row_count,col,row[col],headerStyle) elif row_count == 3: sheet.write(row_count,col,row[col],subheadStyle) elif "Assets" in row[col]: sheet.write(row_count,col,row[col],highlightStyle) else: if (is_number(row[col]) == True): sheet.write(row_count,col,float(row[col]),rowStyle) else: sheet.write(row_count,col,row[col],rowStyle) 正如你所看到的,根据行我使用不同的风格。 我怎样才能使它包含关键字“资产”的任何行将突出显示? 谢谢!

如何在使用Python添加数据时保留Excel中的macrosbutton

对于我正在维护的一个过程,我有一个创buildcsv文件的脚本,然后将csv文件复制到带有激活macros的button的Excel工作簿中。 这个过程工作得很好。 我正试图通过编写一个脚本来直接构build工作簿,从而消除了一个步骤,从而改善了这一过程。 我认为最好的办法是创build一个模板工作簿,其中第一个工作表具有macrosbutton。 然后,我将简单地复制模板工作簿,添加我的数据并将新工作簿保存在新的自定义名称下。 我的testing代码如下: import csv, os, sys, xlrd, xlwt, xlutils, shutil from copy import deepcopy from xlutils import save from xlutils.copy import copy templatefile = 'N:\Tools\Scripts-DEV\Testing_Template.xls' Destfile = 'N:\Tools\Scripts-DEV\Testing_Dest.xls' shutil.copy(templatefile,Destfile) # Works fine up to here. # If you look at the new file, it has the button that is in the […]

尝试使用python在xlwt中设置单元格颜色时丢失属性错误

我正在尝试使用python的xlwt包将我的Excel电子表格的颜色更改为颜色代码数据。 这是我现在的代码: from xlrd import open_workbook from tempfile import Temporary File from xlwt import sys import csv …. ….#Some other code style = xlwt.XFStyle() #this is the line throwing the error pattern = xlwt.Pattern() pattern.pattern = xlwt.Pattern.SOLID_PATTERN pattern.pattern_fore_colour = xlwt.Style.colour_map['red'] pattern.pattern_fore_colour = xlwt.Style.colour_map['red'] newSheet.write(row, col, values[row – 1][col], pattern) …. ….#Some other code 其他代码段在那里,因为它是一个比所示的更长的脚本,但这些代码段与这个问题没有关系 运行时会得到以下堆栈跟踪: […]

如何在Excel文件中添加超链接到单元格的内容?

我从Excel数据库中导出数据: response = HttpResponse(mimetype="application/ms-excel") response['Content-Disposition'] = 'attachment; filename=Countries.xls' wb = xlwt.Workbook() ws = wb.add_sheet('Countries') ws.write(0, 0, 'Country ID') ws.write(0, 1, 'Country Name') index = 1 for country in countries: ws.write(index, 1, country.country_id) ws.write(index, 1, country.country_name) index += 1 wb.save(response) return response 它导出我的Excel文件。 如何在此文件中添加超链接到单元格的内容? ( country_name例如是在浏览器中打开卡片的链接)

在Excel中打印输出

我是Python的新手。 我想从多个XML文件中检索标签值,并将其打印在Excel表格中。 我试过并得到了除了Excel打印部分以外的脚本工作正常。 这是我的脚本 from xml.dom.minidom import parse, parseString import xlwt import os def sh(dir): for r,d,f in os.walk(dir): n=0 for files in f: if files.endswith(".xml"): print files dom=parse(os.path.join(r, files)) name = dom.getElementsByTagName('rev') title = dom.getElementsByTagName('title') a=xlwt.Workbook() sheet=a.add_sheet('sheet1') sheet.write(n, 0, files) sheet.write(n, 1, title[0].firstChild.nodeValue) sheet.write(n, 2, name[0].firstChild.nodeValue) n=n+1 a.save('sha.xls') print title[0].firstChild.nodeValue print name[0].firstChild.nodeValue sh("path") 我坚持的问题是,输出仅在这些列(0,0),(0,1),(0,2)中打印。 […]

Python xlwt库中的set_style对于包含文本的单元格不起作用

我正在使用Python的Excel电子表格。 如果条件不被遵守,我想改变整行的背景颜色。 但是,在运行我的代码后,只有行的空单元格(不包含任何字符)的背景颜色被改变。 我的前9列包含信息,我的代码只改变从J栏到Z的背景颜色。 from xlrd import open_workbook from xlwt import Workbook, easyxf Error_Style = easyxf('pattern: pattern solid, fore_colour red;',) […] else: w_sheet.row(row_index).set_style(Error_Style) […] 我想知道如果我使用easyxf的错误模式。

Python XLWT – 自定义格式的问题

我正在努力把时间放到一个牢房里。 我需要它是独立的,无需年,月,日数据date时间追加。 当我尝试使用格式h:mm AM / PM写入string15:55:00时 ,它显示为excel表格上的15:55:00 ,直到我选中该单元格并按回车键,所以看起来这不是有效的方法来做到这一点。 通过date时间的工作,但又一次,我不想有我的单元格中的date信息,只是格式正确的时间。 我目前的代码如下: style = XFStyle() style.num_format_str = 'h:mm:ss AM/PM' time = "15:22:00" ws.write(i, 4, time, style)

我如何在Python中开始我的第一个项目? (比较date,使用文件和xlrd)

我对Python非常了解,并且对我的第一个项目有了一个想法,但是由于我的经验不足,看起来很复杂。 可能你们可以帮我弄清楚如何devise它,使其更简单或更高效。 我每周都会收到这些电子邮件,一个XLS文件,整个星期的业务运作。 下面是关于XLS如何的一个虚构的例子。 03/12/2014 04/12/2014 05/12/2014 Initial cash 20000 19000 19800 account receivable 30 50 60 account payable 40 60 90 net cash 20000 19800 19500 我打算做的是,每次从特定的文件夹中读取这个XLS文件,并在一个单独的文本文件上build立一个日常操作数据库(我select输出到一个文本文件,因为我认为这对我来说更容易写入比用XLWT模块编写另一个XLS文件)。 输出文本文件应该看起来像这样(所以你可以有一个想法), 20/10/2014 A/C 75200051479 这是我的问题: 我想让程序从文本文件中检查最后一行的最后一个date,比较第一行的date是从XLS追加的。 这是可行的,如何? 我应该输出到一个元组,一个列表或一个简单的string,使我更容易提取date,并将其与XLS的第一行进行比较以追加。 我的例子是一种CSV文件。 我应该如何看待代码的顺序:打开TXT,获取date数据,使用xlrd比较date和XLS,使用xlrd从XLS获取数据并将数据附加到文本文件。 感谢您通过这篇文章,我很感激任何帮助,因为我从Python 3.3.5开始。

XLWT转换为excel文件

在我的应用程序中,我试图用xlwt将数据写入excel文件。 源代码: # -*- coding: utf-8 -*- import sys,os from xlwt import Workbook, easyxf, Formula #define the extension print """Extension supported: xls, xlsx, ods""" ext=raw_input("Extension: ") def export2excel(): '''Edit the OK.txt file to add new lines following the upper lines ''' fp = file('~/OK.txt') lines = fp.readlines() wb = Workbook() ws = wb.add_sheet('Test') ulstyle = […]

将结果写入.xls(将2个查询提交到网页,并将不同的结果存储到.xls中)

大家好…我正在使用Python 2.76将查询提交到.aspx网页,并通过BeautifulSoup获取结果,并且希望将它们存储到Excel电子表格中。 import mechanize import re import xlwt from bs4 import BeautifulSoup import urllib2 book = xlwt.Workbook(encoding='utf-8', style_compression = 0) sheet = book.add_sheet('Legi', cell_overwrite_ok = True) for items in ['university student', 'high school student']: url = r'http://legistar.council.nyc.gov/Legislation.aspx' request = mechanize.Request(url) response = mechanize.urlopen(request) forms = mechanize.ParseResponse(response, backwards_compat=False) form = forms[0] response.close() form['ctl00$ContentPlaceHolder1$txtSearch'] = items submit_page […]