I just want save data in Excel, I found this code and it worked perfectly:
import xlsxwriter
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('zzzzz.xlsx')
worksheet = workbook.add_worksheet()
date = "17/08/2015"
bill = 001
item = "something"
customer = "Luis"
price = 100
# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
worksheet.write(row, col, date )
worksheet.write(row, col + 1, bill)
worksheet.write(row, col + 2, item )
worksheet.write(row, col + 3, customer )
worksheet.write(row, col + 4,price)
workbook.close()
The problem I have are when I try to save info with spaces, if:
customer = "JOSE LUIS FEBRERO LOPEZ"
error:
>>>
Traceback (most recent call last):
File "C:\Python27\crearexcel.py", line 28, in <module>
workbook.close()
File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 296, in close
self._store_workbook()
File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 520, in _store_workbook
xml_files = packager._create_package()
File "C:\Python27\lib\site-packages\xlsxwriter\packager.py", line 140, in _create_package
self._write_shared_strings_file()
File "C:\Python27\lib\site-packages\xlsxwriter\packager.py", line 280, in _write_shared_strings_file
sst._assemble_xml_file()
File "C:\Python27\lib\site-packages\xlsxwriter\sharedstrings.py", line 53, in _assemble_xml_file
self._write_sst_strings()
File "C:\Python27\lib\site-packages\xlsxwriter\sharedstrings.py", line 83, in _write_sst_strings
self._write_si(string)
File "C:\Python27\lib\site-packages\xlsxwriter\sharedstrings.py", line 110, in _write_si
self._xml_si_element(string, attributes)
File "C:\Python27\lib\site-packages\xlsxwriter\xmlwriter.py", line 122, in _xml_si_element
self.fh.write("""<si><t%s>%s</t></si>""" % (attr, string))
File "C:\Python27\lib\codecs.py", line 694, in write
return self.writer.write(data)
File "C:\Python27\lib\codecs.py", line 357, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 11: ordinal not in range(128)
I tried to convert to string str(customer) but that doesn't work.
The only solution I found are replace spaces " " for "_"
Any idea how I can save data with spaces in Excel?
This all my code, I get all files from folder, later I convert to text, after extract some data and later I try to create a excel file.
I try to coding utf-8 but don`t work
# -*- coding: cp1252 -*-
# -*- coding: UTF-8 -*-
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from cStringIO import StringIO
import os
import xlsxwriter
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('zzzzz.xlsx')
worksheet = workbook.add_worksheet()
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
z = 0
e = (len(files) - 1)
def convert_pdf_to_txt(path):
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = file(path, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos=set()
fstr = ''
for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
interpreter.process_page(page)
str = retstr.getvalue()
fstr += str
fp.close()
device.close()
retstr.close()
return fstr
row = 0
col = 0
while z<e:
factura = files[z]
#ejemplo 1
string = convert_pdf_to_txt(factura)
lines = list(filter(bool,string.split('\n')))
custData = {}
for i in range(len(lines)):
if 'EMAIL:' in lines[i]:
custData['Name'] = lines[i+1]
elif 'FACTURA' in lines[i]:
custData['BillNumber'] = lines[i+1]
elif 'Vencimientos:' in lines[i]:
custData['price'] = lines[i+2]
elif 'Banco:' in lines[i]:
custData['paymentType'] = lines[i+1]
#ejemplo 2
txtList = convert_pdf_to_txt(factura).splitlines()
nameIdx, billNumIdx, priceIdx, expirDateIdx, paymentIdx = -1, -1, -1, -1, -1
for idx, line in enumerate(txtList):
if line == "EMAIL: [email protected]":
nameIdx = idx +2 # in your example it should be +2...
if line == "FACTURA":
billNumIdx = idx + 1
if "Vencimientos:" in line:
priceIdx = idx + 2
expirDateIdx = idx + 1
if "Banco:" in line:
paymentIdx = idx + 1
name = txtList[nameIdx] if nameIdx != -1 else ''
billNum = txtList[billNumIdx] if billNumIdx != -1 else ''
price = txtList[priceIdx] if priceIdx != -1 else ''
expirDate = txtList[expirDateIdx] if expirDateIdx != -1 else ''
payment = txtList[paymentIdx] if paymentIdx != -1 else ''
print expirDate
billNum = billNum.replace("Â Â ", "")
print billNum
custData['Name'] = custData['Name'].replace("Â", "")
print custData['Name']
custData['paymentType'] = custData['paymentType'].replace("Â", "")
print custData['paymentType']
print price
nombre = str(custData['Name'])
formadepago = custData['paymentType']
z+=1
columna2 = str(billNum) + ", " + nombre + ", " + formadepago
worksheet.write(row, col, expirDate)
worksheet.write(row, col + 1, columna2)
worksheet.write(row, col + 2, price)
row+=1
workbook.close()
Hi, I add this code after all import.... and now I'm able to save nonbreaking space.
import sys
reload(sys)
sys.setdefaultencoding('Cp1252')
sys.setdefaultencoding('Cp1252') - this is a nasty fix-all hack. It masks other issues and makes your code brittle and platform specific.
"JOSE LUIS FEBRERO LOPEZ" contains a Windows-1252 encoded Non-breaking space (0xA0).
xlsxwriter requires that you pass Unicode objects when using non-ASCII characters. In fact, it's good practice to use Unicode objects for all strings.
Create a Unicode object by appending a u to the front of the string:
u"JOSE LUIS FEBRERO LOPEZ"
As your Python source code is encoded as Windows-1252, you will need to add the following to the top of your source file:
# coding=cp1252
This will tell Python how to decode the strings in your source file into Unicode objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With