Tag: python

使用Openpyxl获取一列数据并生成math操作列表

我是一个python新手我想在python 2.7中使用openpyxl模块来获得一列数字,把它们放在一个列表中,并将它们添加到。 我无法使用for循环,因为它在每个循环中给出了更新的列表。 我怎样才能得到“最终”列表只使用其他方法? 这里是代码: import openpyxl ac = openpyxl.load_workbook('Financial Sample.xlsx') sheet = ac.get_sheet_by_name('Sheet1') l=[] sum = 0 for i in range(2,10): sub = int(sheet.cell(row=i, column=7).value) l.append(sub) print(l) #for x in []: #sum = sum + x #print(sum) 谢谢!

通过Python在Excel中保存富文本格式

我试图通过Python将行添加到Excel文件(需要这个每天运行和刷新)。 Excel文件本质上是一个模板,在其顶部有一些单元格中的某些单元格具有特定的格式,即单元格值“那只猫是蓬松的 ”。 我不能完全find一种方法来让Python + Excel一起工作来保持格式化 – 它采用单元格中第一个字母的格式,并将其应用于整个板上。 从我可以告诉,这是一个保留丰富的文本的问题,但我一直没能find一个可以保存丰富的文本,读写excel文件的包。 我跟着这个线程拿出下面的代码: 使用xlwt写入现有的工作簿 但是,它看起来像从xlutils包复制步骤不保留富文本格式。 import xlwt import xlrd from xlutils.copy import copy rb = xlrd.open_workbook(templateFile,formatting_info=True) r_sheet = rb.sheet_by_index(0) wb = copy(rb) w_sheet = wb.get_sheet(0) xlsfile = Infile insheet = xlrd.open_workbook(xlsfile,formatting_info=True).sheets()[0] outrow_idx = 10 for row_idx in xrange(insheet.nrows): for col_idx in xrange(insheet.ncols): w_sheet.write(outrow_idx, col_idx, insheet.cell_value(row_idx, col_idx)) outrow_idx += […]

如何使用xlsxwriter格式化索引列?

我正在使用xlsxwriter和set_column函数来格式化我的excel输出中的列。 但是,应用于索引列(或多索引情况下的索引列)时,格式似乎被忽略。 我find了一个解决方法,到目前为止是引入一个伪索引与reset_index然后传递索引= False to_excel函数,但是那么多索引很好的合并function将不复存在。 有任何想法吗? import pandas as pd import numpy as np from Config import TEMP_XL_FILE def temp(): ' temp' pdf = pd.DataFrame(np.random.randn(6,4), columns=list('ABCD')) pdf.set_index('A', drop=True, inplace=True) writer = pd.ExcelWriter(TEMP_XL_FILE, engine='xlsxwriter') pdf.to_excel(writer, 'temp') workbook = writer.book worksheet = writer.sheets['temp'] tempformat = workbook.add_format({'num_format': '0%', 'align': 'center'}) worksheet.set_column(-1, 3, None, tempformat) writer.save() if __name__ == […]

使用Excel Com对象的Python线程

我试图打开一个工作簿使用python线程内预先打开的Excel COM对象。 使用下面的代码: from multiprocessing import Process, Queue def open_workbook(excel,iSub_Loc,q): p = Process(target = open_workbook_helper, args = (excel,iSub_Loc)) p.daemon = True p.start() def open_workbook_helper(excel,iSub_Locq,): wb = excel.Workbooks.Open(iSub_Loc) ws = wb.Sheets(1) q.put((wb,ws)) 但我得到以下错误 Can't pickle <type PyIDispatch'>: it's not found as __builtin__.PyIDispatch 有什么build议么?

读取excel并将索引转换为datatimeindexpandas

我读了这样一个pandas的excel df = pd.read_excel("Test.xlsx", index_col=[0]) 数据框的外观与包含date和时间以及一列的索引类似: 01.01.2015 00:15:00 47.2 01.01.2015 00:30:00 46.6 01.01.2015 00:45:00 19.4 01.01.2015 01:00:00 14.8 01.01.2015 01:15:00 14.8 01.01.2015 01:30:00 16.4 01.01.2015 01:45:00 16.2 … 我想将索引转换为datatimeindex,我试过了 df.index = pd.to_datetime(df.index) 并得到:“ValueError:未知的string格式” 这里最好的方法是将索引转换为包含date和tiem的数据时间格式,以使用基于datetime的函数

Python与Openpyxl。 公式分析错误。 意想不到的字符

我准备准备一些导出Excel文件的工具。 我有一个文件模板,当我用openpyxl加载它,我得到一个错误: File "D:\Python\lib\site-packages\openpyxl-2.3.5-py2.7.egg\openpyxl\reader\excel.py", line 234, in load_workbook parser.parse() File "D:\Python\lib\site-packages\openpyxl-2.3.5-py2.7.egg\openpyxl\reader\worksheet.py", line 106, in parse dispatcher[tag_name](element) File "D:\Python\lib\site-packages\openpyxl-2.3.5-py2.7.egg\openpyxl\reader\worksheet.py", line 243, in parse_row_dimensions self.parse_cell(cell) File "D:\Python\lib\site-packages\openpyxl-2.3.5-py2.7.egg\openpyxl\reader\worksheet.py", line 172, in parse_cell value = trans.translate_formula(coordinate) File "D:\Python\lib\site-packages\openpyxl-2.3.5-py2.7.egg\openpyxl\formula\translate.py", line 145, in translate_formula tokens = self.get_tokens() File "D:\Python\lib\site-packages\openpyxl-2.3.5-py2.7.egg\openpyxl\formula\translate.py", line 54, in get_tokens self.tokenizer.parse() File "D:\Python\lib\site-packages\openpyxl-2.3.5-py2.7.egg\openpyxl\formula\tokenizer.py", line 83, in parse self.offset […]

使用Python和Openpyxl循环一个.xlsx,但循环只保存最后一行的数据

我是一个Python的新手,我正在一个项目,以自动化一个非常耗时的项目。 我使用的是openpyxl来访问.xlsx来提取信息,最终将转换成距离和方向轴承与arcpy / arcgis一起使用,这意味着我正在使用Python 2.7。 我可以访问数据并进行第一轮更改,但无法将我的写入命令集成到我的循环中。 目前它将最后一行的数据保存到新的.xlsx中给定范围内的所有单元格中。 这是我的代码: #Importing OpenPyXl and loads the workbook and sheet import openpyxl wb = openpyxl.load_workbook('TESTVECT.xlsx') ws = wb.get_sheet_by_name('TEST') #allows to save more than once write_only = False cell_range = ws['C'] #sorts through either the rows/columns and slices the required string maxRow = ws.max_row + 1 for row in range(2, […]

需要帮助如何使用文本或Excel中的所有值进行相同的计算并将其导出

Sup伙计,好吧,我有一个很大的问题。 首先我是python新手(atm 3.x)。 我需要一个帮助,弄清楚,如何正确select正确的数据进行计算(对不起,我的英语)。 例如:我有一个Excel数据文件: 我的Excel数据看起来像这样 我可以数例如手动例如 book = openpyxl.load_workbook('angles.xls') sheet = book.get_sheet_by_name('Sheet1') angle = sheet['A1'].value minute = sheet['B1'].value seconds = sheet['C1'].value calculations = angle + minute/60 + seconds/60 也出口这个 sys.stdout = open(input("name it :"), 'w') print(calculations) 但我不知道如何使自动计算下一个值,A2,B2,C2 ….. A7,B7,C7 ……直到所有现有的值。 毕竟,而不是1个结果,我可以得到所有。 例如,如果从A1,B1,C1 …….. A10,B10,C10进行了10次计算并得到10次计算。 如果它更容易,它也可以是一个文本数据。 感谢您的时间。

不在scrapy中创build和编写xlxswriter包中的excel

我正在写一个代码来抓取网站与pythonselenium代码执行和打印细节我想要的,但我想存储在excel文件中使用python xlxswriter。 这是我的代码 import xlsxwriter import time from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.by import By row = 0 col = 0 r=1 if r: workbook = xlsxwriter.Workbook('fit1.xlsx') worksheet = workbook.add_worksheet() for i in range(1,3): url = 'http://www.examble.com/kids//page-%d.html' % i for j […]

将数据附加到格式化的Excel中

我想附加一些数据到Excel文件中: import os from xlutils.copy import copy import xlrd as ExcelRead def write_append(file_name): values = ["Ann", "woman", 22, "UK"] r_xls = ExcelRead.open_workbook(file_name,formatting_info=True,on_demand=True) r_sheet = r_xls.sheet_by_index(0) rows = r_sheet.nrows w_xls = copy(r_xls) sheet_write = w_xls.get_sheet(0) for i in range(0, len(values)): sheet_write.write(rows, i, values[i]) w_xls.save(file_name) if __name__ == "__main__": write_append("./bzxx.xls") 但得到的错误结果如下: D:\Python\Environment\env_finance\Scripts\python.exe E:/Project/Finance/code/financeSystem/temp/copyfile.py Traceback (most recent call […]